We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
John and Fences
John and Fences
Sort by
recency
|
10 Discussions
|
Please Login in order to post a comment
scala:
Is it availble for Fence Cleaning Atlanta website? I want to integrate it on my website.
My solution i find by scala
import java.io._ import scala.collection.mutable.ArrayBuffer import scala.math
object Solution { def main(args: Array[String]) { val rd = new BufferedReader(new InputStreamReader(System.in)) val N = rd.readLine().toInt val h = Array(0) ++ rd.readLine().split(' ').map(_.toInt) ++ Array(0) var l: ArrayBuffer[Int] = ArrayBuffer.fill(h.length)(-1) var r: ArrayBuffer[Int] = ArrayBuffer.fill(h.length)(-1) for (i <- 1 until h.length - 1) { var ll: Int = i - 1 if (h(ll) < h(i)) { l(i) = i } else { while (ll != -1 && h(ll) >= h(i)) { l(i) = ll if (ll == l(ll)) ll = ll - 1 else ll = l(ll) } } } for (i <- h.length - 2 to 1 by -1) { var rr: Int = i + 1 if (h(rr) < h(i)) { r(i) = i } else { while (rr != -1 && h(rr) >= h(i)) { r(i) = rr if (rr == r(rr)) rr = rr + 1 else rr = r(rr) } } } var ans = 0 for (i <- 1 until h.length - 1) { ans = math.max(ans, (r(i) - l(i) + 1) * h(i)) } println(ans) } }
Got pretty close, think the algorithm is correct, I think I just need to make some optimizations to the stuff i'm caching / rechecking:
Did anyone make passing Scala solution? Mime timeouts on 5 to 10. I will try to port my solution to Haskell...
yes, my approach is simply count the number of that bigger or equal to the i-th element for each i. It passes the testcases. But there is a more efficent approach for this problem by maintain a non-decreasing stack.
`