Last active
March 19, 2021 20:03
-
-
Save jsparadacelis/881d23ca391b221f347092eaa481f485 to your computer and use it in GitHub Desktop.
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
water_arr = [4, 2, 0, 3, 2, 5] | |
def water_area(water_arr): | |
left_max = [] | |
right_max = [] | |
wa_idx_left = 0 | |
wa_idx_right = len(water_arr) | |
while wa_idx_left < len(water_arr) and wa_idx_right >= 0: | |
highest_left = max(water_arr[:wa_idx_left]) if water_arr[:wa_idx_left] else 0 | |
left_max.append(highest_left) | |
highest_right = max(water_arr[wa_idx_right:]) if water_arr[wa_idx_right:] else 0 | |
right_max.append(highest_right) | |
wa_idx_left +=1 | |
wa_idx_right -=1 | |
right_max = right_max[::-1] | |
total_area = 0 | |
for left, right, idx in zip(left_max, right_max, water_arr): | |
smallest_pillar = min(left, right) | |
if left > idx < right: | |
total_area += abs(smallest_pillar-idx) | |
return total_area |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment