Skip to content

Commit

Permalink
Create: 0011-container-with-most-water.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSWE committed Jan 6, 2023
1 parent 59f24eb commit 80d28c8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions scala/0011-container-with-most-water.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

object Solution {
def maxArea(height: Array[Int]): Int = {
var maxWater = 0
var left = 0
var right = height.length - 1

while (left < right) {
var minHeight = height(left).min(height(right))
var dist = right - left
var capacity = minHeight * dist
if (capacity > maxWater) {
maxWater = capacity
}

if (height(left) < height(right)){
left += 1
}
else {
right -= 1
}
}

return maxWater
}
}

0 comments on commit 80d28c8

Please sign in to comment.