Sort by

recency

|

10 Discussions

|

  • + 0 comments

    scala:

    import scala.collection.mutable.Stack
    import scala.io.StdIn.readLine
    
    object Solution {
      private def carve(heights: Seq[Int]): Int = {
        val extendedHeights = heights :+ 0
        val stack = Stack[Int]()
        var maxArea = 0
        for (i <- extendedHeights.indices) {
          while (stack.nonEmpty && extendedHeights(i) < extendedHeights(stack.top)) {
            val height = extendedHeights(stack.pop())
            val width = if (stack.isEmpty) i else (i - stack.top - 1)
            maxArea = math.max(maxArea, height * width)
          }
          stack.push(i)
        }
        maxArea
      }
    
      def main(args: Array[String]): Unit = {
        readLine()
        val heights = readLine().trim.split(' ').map(_.toInt).toSeq
        println(carve(heights))
      }
    }
    
  • + 0 comments

    Is it availble for Fence Cleaning Atlanta website? I want to integrate it on my website.

  • + 1 comment

    My solution i find by scala

  • + 0 comments

    Got pretty close, think the algorithm is correct, I think I just need to make some optimizations to the stuff i'm caching / rechecking:

    (let [line1 (read-line)]
        (let [fences (map read-string (clojure.string/split (read-line) #" "))]
            (println
                ((fn[x cache stopped-cache]
                    (if (= x (count fences))
                        (apply max (map (fn[x] (x :valu)) (into cache stopped-cache)))
                        (let [fence (nth fences x)]
                            (recur (+ 1 x)
                                (conj (map (fn[x]
                                    (let [m (min (x :mini) fence)
                                        newWidth (+ 1 (x :width))
                                        newValu (* newWidth m)]
                                        {:valu newValu :mini m :width newWidth}
                                    )
                                ) cache) {:valu fence :mini fence :width 1})
                                (into stopped-cache cache)
                            )
                        )
                    )
                ) 0 [{:valu 0 :mini Integer/MAX_VALUE :width 0 }] [])
            )
        )
    )
    
  • + 1 comment

    Did anyone make passing Scala solution? Mime timeouts on 5 to 10. I will try to port my solution to Haskell...