|
| 1 | +''' |
| 2 | +Descripttion: 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 |
| 3 | +version: 1 |
| 4 | +Author: Jason |
| 5 | +Date: 2020-12-07 10:35:20 |
| 6 | +LastEditors: Jason |
| 7 | +LastEditTime: 2020-12-07 16:10:25 |
| 8 | +''' |
| 9 | + |
| 10 | + |
| 11 | +from typing import List |
| 12 | +import random |
| 13 | + |
| 14 | + |
| 15 | +def GenerateRandomList(number, size): |
| 16 | + temp = list() |
| 17 | + random_legth = random.randint(0, size) |
| 18 | + current_length = 0 |
| 19 | + while current_length < random_legth: |
| 20 | + temp.append(random.randint(1, number)) |
| 21 | + current_length += 1 |
| 22 | + return temp |
| 23 | + |
| 24 | + |
| 25 | +class Solution: |
| 26 | + def trap(self, height: List[int]): |
| 27 | + left = 0 |
| 28 | + right = len(height) - 1 |
| 29 | + lower = 0 |
| 30 | + level = 0 |
| 31 | + res = 0 |
| 32 | + while left < right: |
| 33 | + if height[left] < height[right]: |
| 34 | + lower = height[left] |
| 35 | + left += 1 |
| 36 | + else: |
| 37 | + lower = height[right] |
| 38 | + right -= 1 |
| 39 | + level = lower if lower > level else level |
| 40 | + res += (level - lower) |
| 41 | + return res |
| 42 | + |
| 43 | + def trap2(self, height: List[int]): |
| 44 | + ''' |
| 45 | + func: 按行求 |
| 46 | + param {*} |
| 47 | + return {*} |
| 48 | + ''' |
| 49 | + length = len(height) |
| 50 | + res = 0 |
| 51 | + for index in range(1, length - 1): |
| 52 | + leftMax = 0 |
| 53 | + for index_left in range(index): |
| 54 | + if height[index_left] > leftMax: |
| 55 | + leftMax = height[index_left] |
| 56 | + rightMax = 0 |
| 57 | + for index_right in range(index + 1, length): |
| 58 | + if height[index_right] > rightMax: |
| 59 | + rightMax = height[index_right] |
| 60 | + lower = min(leftMax, rightMax) |
| 61 | + if lower > height[index]: |
| 62 | + res += (lower - height[index]) |
| 63 | + return res |
| 64 | + |
| 65 | + def trap3(self, height: List[int]): |
| 66 | + ''' |
| 67 | + func: 从左往右扫一遍,找到每一个值左侧的最大;从右往左扫一遍,找到每一个数右侧最大的值, |
| 68 | + param {*} |
| 69 | + return {*} |
| 70 | + ''' |
| 71 | + length = len(height) |
| 72 | + if length < 3: |
| 73 | + return 0 |
| 74 | + leftMax = [0] |
| 75 | + rightMax = [0] |
| 76 | + point_left = 1 |
| 77 | + point_right = length - 2 |
| 78 | + while point_left < length: |
| 79 | + # 添加leftMax |
| 80 | + if height[point_left - 1] > leftMax[-1]: |
| 81 | + leftMax.append(height[point_left - 1]) |
| 82 | + else: |
| 83 | + leftMax.append(leftMax[-1]) |
| 84 | + |
| 85 | + # 添加rightMax |
| 86 | + if height[point_right + 1] > rightMax[-1]: |
| 87 | + rightMax.append(height[point_right + 1]) |
| 88 | + else: |
| 89 | + rightMax.append(rightMax[-1]) |
| 90 | + |
| 91 | + point_left += 1 |
| 92 | + point_right -= 1 |
| 93 | + |
| 94 | + res = 0 |
| 95 | + for i in range(length): |
| 96 | + lower = min(leftMax[i], rightMax[length - 1 - i]) |
| 97 | + if lower > height[i]: |
| 98 | + res += (lower - height[i]) |
| 99 | + return res |
| 100 | + |
| 101 | + |
| 102 | +s = Solution() |
| 103 | +for _ in range(100): |
| 104 | + li = GenerateRandomList(20, 20) |
| 105 | + if len(li) < 1: |
| 106 | + continue |
| 107 | + res1 = s.trap(li) |
| 108 | + res2 = s.trap2(li) |
| 109 | + res3 = s.trap3(li) |
| 110 | + if res1 != res3: |
| 111 | + print("*********************************") |
| 112 | + print("Wrong") |
| 113 | + print(li) |
| 114 | +print("Done") |
0 commit comments