GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/042_Trapping_Rain_Water.py at master · Weihouye/leetcode Expand file tree/ Copy path042_Trapping_Rain_Water.pyMore file actions 76 lines (70 loc) · 2.29 KB / Copy path042_Trapping_Rain_Water.pyFile metadata and controls76 lines (70 loc) · 2.29 KB 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 class Solution(object): def trap(self, height): """ :type height: List[int] :rtype: int """ ls = len(height) if ls == 0: return 0 res, left = 0, 0 while left < ls and height[left] == 0: left += 1 pos = left + 1 while pos < ls: if height[pos] >= height[left]: # there is a right bar which is no less than left bar res += self.rain_water(height, left, pos) left = pos pos += 1 elif pos == ls - 1: # left bar is higher than all right bar max_value, max_index = 0, pos for index in range(left + 1, ls): if height[index] > max_value: max_value = height[index] max_index = index res += self.rain_water(height, left, max_index) left = max_index pos = left + 1 else: pos += 1 return res def rain_water(self, height, start, end): # computer rain water if end - start <= 1: return 0 min_m = min(height[start], height[end]) res = min_m * (end - start - 1) step = 0 for index in range(start + 1, end): if height[index] > 0: step += height[index] return res - step # def trap(self, height): # ls = len(height) # if ls == 0: # return 0 # height.append(0) # height.insert(0, 0) # left = [0] * ls # right = [0] * ls # cur_left, cur_right = 0, 0 # for i in range(1, ls + 1): # cur_left = max(cur_left, height[i - 1]) # # left[i] store max bar from left # left[i - 1] = cur_left # for i in reversed(range(ls)): # cur_right = max(cur_right, height[i + 1]) # # right[i] store max bar from right # right[i] = cur_right # res = 0 # for i in range(ls): # curr = min(left[i], right[i]) # if curr > height[i]: # res += curr - height[i] # return res if __name__ == '__main__': # begin s = Solution() print s.trap([2,6,3,8,2,7,2,5,0]) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |