File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed
kotlin/src/main/kotlin/common/twopointers/largestcontainer Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -10,19 +10,19 @@ class LargestContainer {
10
10
var left = 0
11
11
var right = heights.size - 1
12
12
while (left < right) {
13
- // Select minimum among them. Water can be contained depending on lower height.
14
- val width = min(heights[left], heights[right])
15
- val length = right - left
16
- // Rectangle formula.
17
- // Calculate the water contained between the current pair of lines.
18
- val water = width * length
13
+ // Calculate the water contained between the current pair of
14
+ // lines.
15
+ val water = min(heights[left], heights[right]) * (right - left)
19
16
maxWater = max(maxWater, water)
20
-
21
- // Move the pointers inward, always moving the pointer at the shorter line.
22
- // Otherwise, just move the right side .
17
+ // Move the pointers inward, always moving the pointer at the
18
+ // shorter line. If both lines have the same height, move both
19
+ // pointers inward .
23
20
if (heights[left] < heights[right]) {
24
21
left++
22
+ } else if (heights[left] > heights[right]) {
23
+ right--
25
24
} else {
25
+ left++
26
26
right--
27
27
}
28
28
}
You can’t perform that action at this time.
0 commit comments