-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRectangle.scala
62 lines (43 loc) · 1.66 KB
/
LargestRectangle.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io._
import scala.collection.mutable
//https://www.hackerrank.com/challenges/largest-rectangle/problem
object LargestRectangle {
case class Building(index: Int, height: Int)
/*
Use a stack to keep track of previous buildings.
If current building's height is > than prev,
we now that max size is either currents height * all prev higher buildings
or previous building height * all prev higher buildings.
Once we have traveled all previous higher buildings
we add the current height associated with lowest index that is higher than current.
Finally, if stack isn't empty we just travel it using its info to calculate area,
we are sure that remaining stack buildings are lower than any to their right.
*/
def largestRectangle(h: Array[Int]): Long = {
val heights = mutable.Stack[Building]()
var answ = 0
for((num, i) <- h.zipWithIndex) {
var left = i
while(heights.nonEmpty && heights.top.height > num) {
val prev = heights.pop()
answ = math.max(answ, num*(i + 1 - prev.index))
answ = math.max(answ, prev.height * (i - prev.index))
left = prev.index
}
heights.push(Building(left, num))
}
heights.foreach (
(building) => answ = math.max(answ, building.height * (h.length - building.index))
)
answ
}
def main(args: Array[String]) {
val stdin = scala.io.StdIn
val printWriter = new PrintWriter(new OutputStreamWriter(System.out))
val n = stdin.readLine.trim.toInt
val h = stdin.readLine.split(" ").map(_.trim.toInt)
val result = largestRectangle(h)
printWriter.println(result)
printWriter.close()
}
}