Sort by

recency

|

88 Discussions

|

  • + 1 comment
    import java.util.Scanner
    
    import scala.collection.immutable.Queue
    
    object Solution {
      def main(args: Array[String]): Unit = {
        val sc = new Scanner(System.in)
    
        sc.nextInt
        val k = sc.nextInt
        sc.nextLine
        val a = sc.nextLine.split(" ").map(_.toInt)
    
        sc.close()
    
        case class Acc(list: List[Int])
        val sentinel = Int.MaxValue / 2
        val temp = a.foldLeft(Acc(List(-sentinel, sentinel)))((acc, v) => acc.list match {
          case Nil if v < 0 => acc
          case Nil => Acc(v :: Nil)
          case list@x :: xs => if ((v < 0) == (x < 0)) Acc(x + v :: xs) else Acc(v :: list)
        }).list
    
        val compressed = if (temp.head < 0) temp.tail else temp
    
        println(solve(compressed)
          .sortBy(-_)
          .take(k)
          .mkString("\n")
        )
      }
    
      def solve(seq: List[Int]): List[Int] = {
        case class Accumulator(totalSum: Int, queue: Queue[Int], answer: List[Int])
    
        @scala.annotation.tailrec
        def inner(seq: List[Int], acc: Accumulator): List[Int] = {
          seq match {
            case Nil =>
              acc.answer
            case xPos :: xs if acc.queue.isEmpty =>
              //empty queue
              inner(xs, Accumulator(xPos, Queue(xPos), acc.answer))
            case xNeg :: xPos :: xs =>
              val (nextSeq, nextAcc) = {
                val sNeg = acc.totalSum + xNeg
                if (sNeg <= 0) {
                  val (firstPos, tempQueue) = acc.queue.dequeue
    
                  if (tempQueue.isEmpty) {
                    //All items in queue are processed.
                    (xs, Accumulator(xPos, Queue(xPos), firstPos :: acc.answer))
                  } else {
                    if (acc.totalSum >= firstPos) {
                      //Glue items in queue.
                      (xs, Accumulator(xPos, Queue(xPos), acc.totalSum :: acc.answer))
                    } else {
                      //Separate first item in queue.
                      val (_, nextQueue) = tempQueue.dequeue
                      (nextQueue.toList ++ seq, Accumulator(0, Queue(), firstPos :: acc.answer))
                    }
                  }
                } else {
                  val sPos = sNeg + xPos
                  if (sPos >= acc.queue.head) {
                    //Glue
                    (xs, Accumulator(sPos, Queue(sPos), acc.answer))
                  } else {
                    //Uncertain
                    (xs, Accumulator(sPos, acc.queue.enqueue(xNeg).enqueue(xPos), acc.answer))
                  }
                }
              }
    
              inner(nextSeq, nextAcc)
          }
        }
    
        inner(seq, Accumulator(0, Queue(), Nil))
      }
    }
    
  • + 0 comments

    Hint for those who pass all test cases except fail 7, 10, 11, 12 and 16 by which are producing extra numbers and missing small sums:

    Extra sums, credit @boon_low

    • Test case 7 97, 96, 82, 73, 61, 57, 51, 29, 29, 24, 18, 14, 11
    • Test case 10 129, 127, 91, 79, 79, 70, 68, 62, 60, 48, 38, 29, 20, 11, 10
    • Test case 16 136, 126, 99, 93, 53, 49, 49

    My problem was using a left-folding implementation of Kadane's algorithm instead of a right-folding one.

    Consider this, credit @t0mpr1c3

    5 3
    1 -1 999 -2 2
    

    What is the correct answer?

    I thought it was 999, 2, 1 for hours, but the actual correct answer is 999, 2.

  • + 0 comments

    Angle grinders are powerful and versatile but can be dangerous if not used with caution. Overall, it is important to choose the right type of sander for your specific project and to use it with care and attention to detail. click to read

  • + 0 comments

    Looking for an easy-to-use online poster and flyer maker to create eye-catching designs? Look no further than Poster App Lab!

    Our powerful and user-friendly platform offers a vast selection of custom design templates for posters and flyers that will help you create stunning visuals for your business, event, or personal projects. With just a few clicks, you can customise templates, add text, images, and graphics to make them truly unique and stand out from the crowd.

  • + 0 comments

    Great article and a nice way to promote online. I’m satisfied with the information that you provided.