반응형
Problem : leetcode.com/problems/trapping-rain-water/
Solution:
class Solution:
def trap(self, height: List[int]) -> int:
left = right = 0
left_h = []
right_h = []
for value in height:
left = max(value,left)
left_h.append(left)
for value in height[::-1]:
right = max(value,right)
right_h.append(right)
right_h = right_h[::-1]
output =0
for i in range(len(height)):
output += min(left_h[i],right_h[i])-height[i]
return output
and this is time out...
class Solution:
def trap(self, height: List[int]) -> int:
if len(height) != 0:
max_height = max(height)
else:
max_height=0
count = 0
for floor in range(1,max_height+1):
i=0
while True:
if floor <= height[i]:
left_boundary = i
break
i+=1
j=-1
while True:
if floor <= height[j]:
right_boundary = j +len(height)
break
j-=1
for k in range(left_boundary+1, right_boundary):
if height[k]<floor:
count+=1
return count
댓글