Skip to content

Commit 6435b96

Browse files
committed
Adjust algorithm to follow python base structure
1 parent 86ebe80 commit 6435b96

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

kotlin/src/main/kotlin/common/twopointers/largestcontainer/LargestContainer.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ class LargestContainer {
1010
var left = 0
1111
var right = heights.size - 1
1212
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)
1916
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.
2320
if (heights[left] < heights[right]) {
2421
left++
22+
} else if (heights[left] > heights[right]) {
23+
right--
2524
} else {
25+
left++
2626
right--
2727
}
2828
}

0 commit comments

Comments
 (0)