Skip to content

Instantly share code, notes, and snippets.

@kemingy
Last active September 8, 2018 01:04
Show Gist options
  • Save kemingy/f5d8b79a36d095ad0231cf721666407c to your computer and use it in GitHub Desktop.
Save kemingy/f5d8b79a36d095ad0231cf721666407c to your computer and use it in GitHub Desktop.
def contain_water(height):
max_left, max_right = 0, 0
left, right = 0, len(height) - 1
ans = 0
while left < right:
if height[left] < height[right]:
if height[left] > max_left:
max_left = height[left]
else:
ans += max_left - height[left]
left += 1
else:
if height[right] > max_right:
max_right = height[right]
else:
ans += max_right - height[right]
right -= 1
return ans
int maxArea(int* height, int heightSize) {
int start = 0, end = heightSize - 1;
int result = 0, area = 0;
while (start < end) {
area = (end - start) * (height[start] < height[end] ? height[start] : height[end]);
result = (result > area ? result : area);
if (height[start] < height[end]) {
start ++;
} else {
end--;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment