Skip to content

Commit 80d28c8

Browse files
committed
Create: 0011-container-with-most-water.scala
1 parent 59f24eb commit 80d28c8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
object Solution {
5+
def maxArea(height: Array[Int]): Int = {
6+
var maxWater = 0
7+
var left = 0
8+
var right = height.length - 1
9+
10+
while (left < right) {
11+
var minHeight = height(left).min(height(right))
12+
var dist = right - left
13+
var capacity = minHeight * dist
14+
if (capacity > maxWater) {
15+
maxWater = capacity
16+
}
17+
18+
if (height(left) < height(right)){
19+
left += 1
20+
}
21+
else {
22+
right -= 1
23+
}
24+
}
25+
26+
return maxWater
27+
}
28+
}

0 commit comments

Comments
 (0)