-
Notifications
You must be signed in to change notification settings - Fork 24
/
755 - Pour Water.py
38 lines (37 loc) · 1.09 KB
/
755 - Pour Water.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Solution 1: Just follow the logic they give you
# Runtime: O(V*len(heights))
import math
class Solution:
def pourWater(self, heights, V, K):
"""
:type heights: List[int]
:type V: int
:type K: int
:rtype: List[int]
"""
for raining in range(V):
low = heights[K]
low_index = -1
# Try going left
for i in range(K-1,-1,-1):
if heights[i] > low:
break
if heights[i] < low:
low = heights[i]
low_index = i
if low_index > -1:
heights[low_index] += 1
continue
# Try going right
for i in range(K+1,len(heights)):
if heights[i] > low:
break
if heights[i] < low:
low = heights[i]
low_index = i
if low_index > -1:
heights[low_index] += 1
continue
# Raise current
heights[K] += 1
return heights