Created
March 29, 2024 15:00
-
-
Save codertcet111/69e738ce4eb031b73c3778b241cb30ab to your computer and use it in GitHub Desktop.
Leetcode 11: Container with most water storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode 11: Container with most water storage | |
# @param {Integer[]} height | |
# @return {Integer} | |
def max_area(height) | |
i = 0 | |
j = height.length - 1 | |
max_r = 0 | |
while i < j do | |
if height[i] < height[j] | |
curr = height[i] * (j - i) | |
max_r = [max_r, curr].max | |
k = i + 1 | |
while k < j && height[k] < height[i] do | |
k += 1 | |
end | |
i = k | |
else | |
curr = height[j] * (j - i) | |
max_r = [max_r, curr].max | |
k = j - 1 | |
while k > i && height[k] < height[j] do | |
k -= 1 | |
end | |
j = k | |
end | |
end | |
max_r | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment