Sort by

recency

|

14 Discussions

|

  • + 0 comments

    scala using just recursion (tailrec is optional):

    import scala.annotation.tailrec
    import scala.io.StdIn
    
    object Solution {
      private def solve(a: Array[Int], d: Int, m: Int): Int = {
        val n = a.length
    
        def isValid(i: Int, ad: Int, m: Int): Boolean =
          ad <= a(i) && a(i) <= (ad + m)
    
        @tailrec
        def getLeft(i: Int, ad: Int, m: Int): Int = {
          if (i < 0)
            0
          else if (!isValid(i, ad, m))
            (i + 1)
          else
            getLeft(i - 1, ad, m)
        }
    
        @tailrec
        def getRight(i: Int, ad: Int, m: Int): Int = {
          if (n <= i)
            (n - 1)
          else if (!isValid(i, ad, m))
            (i - 1)
          else
            getRight(i + 1, ad, m)
        }
    
        getRight(d + 1, a(d), m) - getLeft(d - 1, a(d), m) + 1
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        in.next()
        val a = in.next().split(' ').map(_.toInt)
        val q = in.next().toInt
        val queries = (1 to q)
          .map(_ => in.next().split(' ').map(_.toInt))
          .map(dm => solve(a, dm.head, dm.last))
          .foreach(println)
      }
    }
    
  • + 0 comments

    scala using memo and precomputing:

    import scala.annotation.tailrec
    import scala.collection.mutable
    import scala.io.StdIn
    
    object Solution {
      val memo = mutable.Map.empty[Int, mutable.Map[Int, Int]]
    
      private def precompute(a: Array[Int], dms: Map[Int, Set[Int]]): Unit = {
        val n = a.length
    
        def isValid(i: Int, ad: Int, m: Int): Boolean =
          ad <= a(i) && a(i) <= (ad + m)
    
        @tailrec
        def getLeft(i: Int, ad: Int, m: Int): Int = {
          if (i < 0)
            0
          else if (!isValid(i, ad, m))
            (i + 1)
          else
            getLeft(i - 1, ad, m)
        }
    
        @tailrec
        def getRight(i: Int, ad: Int, m: Int): Int = {
          if (n <= i)
            (n - 1)
          else if (!isValid(i, ad, m))
            (i - 1)
          else
            getRight(i + 1, ad, m)
        }
    
        def getRange(d: Int, ad: Int, m: Int): Int =
          getRight(d + 1, ad, m) - getLeft(d - 1, ad, m) + 1
    
        dms.foreach { case (d, ms) =>
          memo.addOne(d -> mutable.Map.empty)
          ms.foreach(m => memo(d).addOne(m -> getRange(d, a(d), m)))
        }
      }
    
      private def solve(dm: (Int, Int)): Int = {
        val (d, m) = dm
        memo(d)(m)
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        in.next()
        val a = in.next().split(' ').map(_.toInt)
        val q = in.next().toInt
        val queries = (1 to q)
          .map(_ => in.next().split(' ').map(_.toInt))
          .map(dm => dm.head -> dm.last)
        val dms = queries
          .groupBy(_._1)
          .map(g => g._1 -> g._2.map(_._2).toSet)
          .toMap
        precompute(a, dms)
        queries
          .map(solve)
          .foreach(println)
      }
    }
    
  • + 0 comments

    This code calculates the number of different teams you can form by selecting a certain number of lemurs from a group. It considers scenarios like choosing none or all. The result is given modulo (10^8+7). Thanks for this helpful code; it's been valuable for my stock-related website https://www.nationalpatiocovers.com/

  • + 0 comments

    This problem-solving task for George's stock options is brilliantly presented with a clear input format and concise output requirements. The sample input and output help illustrate the problem effectively. The constraints provide useful information about the problem's scale. Overall, this task is well-structured and engaging, making it easy to understand and implement. Great job!

  • + 0 comments

    Kudos to this problem statement for presenting a real-world scenario and a clear task. It challenges us to find optimal stock options using a well-defined algorithm. A great exercise for coding and problem-solving skills!