|
| 1 | +""" |
| 2 | +In a row of trees, the i-th tree produces fruit with type tree[i]. |
| 3 | +
|
| 4 | +You start at any tree of your choice, then repeatedly perform the following steps: |
| 5 | +
|
| 6 | +1. Add one piece of fruit from this tree to your baskets. If you cannot, stop. |
| 7 | +2. Move to the next tree to the right of the current tree. If there is no tree to the right, stop. |
| 8 | +Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. |
| 9 | +
|
| 10 | +You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. |
| 11 | +
|
| 12 | +What is the total amount of fruit you can collect with this procedure? |
| 13 | +
|
| 14 | + |
| 15 | +
|
| 16 | +Example 1: |
| 17 | +
|
| 18 | +Input: [1,2,1] |
| 19 | +Output: 3 |
| 20 | +Explanation: We can collect [1,2,1]. |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: [0,1,2,2] |
| 24 | +Output: 3 |
| 25 | +Explanation: We can collect [1,2,2]. |
| 26 | +If we started at the first tree, we would only collect [0, 1]. |
| 27 | +Example 3: |
| 28 | +
|
| 29 | +Input: [1,2,3,2,2] |
| 30 | +Output: 4 |
| 31 | +Explanation: We can collect [2,3,2,2]. |
| 32 | +If we started at the first tree, we would only collect [1, 2]. |
| 33 | +Example 4: |
| 34 | +
|
| 35 | +Input: [3,3,3,1,2,1,1,2,3,3,4] |
| 36 | +Output: 5 |
| 37 | +Explanation: We can collect [1,2,1,1,2]. |
| 38 | +If we started at the first tree or the eighth tree, we would only collect 4 fruits. |
| 39 | +
|
| 40 | +
|
| 41 | +对于每一个 i,都会产生 tree[i] 类型的水果。有两个篮子,每个篮子只能放一种类型,但同类型的不限次数。 |
| 42 | +问最多能摘的水果数量。 |
| 43 | +
|
| 44 | +
|
| 45 | +思路: |
| 46 | +1. 一开始用的回溯法: |
| 47 | +
|
| 48 | +用两个变量表示篮子,都有水果时就追加。 |
| 49 | +第三种类型的出现时就进行回溯,回到上一个水果的点再次进行判断。 |
| 50 | +
|
| 51 | +效率上最差就算 O(n²) 吧。 |
| 52 | +反正没passed就是了,90个里过了80个.. |
| 53 | +
|
| 54 | + 1.2. 有个要注意的点:回溯的点选择: |
| 55 | + [1,0,6,6,4,6] |
| 56 | +
|
| 57 | +在 tree[2] (6) 这个点,出现了 1,0,6 三种类型,开始回溯,回溯的点是 0, 6 (1, 2) 。 |
| 58 | +在 tree[4] (4) 这个点,出现了 0,6,4 三种类型,开始回溯,回溯的点需要是 6, 4 (2, 4) 这个6是相邻的第一次出现的点。 |
| 59 | +
|
| 60 | +--- |
| 61 | +
|
| 62 | +2. O(n) 的进阶: |
| 63 | +对于每一个点来说可以存储一些属性来取消回溯: |
| 64 | + [1,0,6,6,4,6] |
| 65 | +
|
| 66 | +count: 这个点可采集到的两种类型的水果数量。 |
| 67 | +repeat_count: 相邻的同类型水果数量。 |
| 68 | +capacity: 篮子里的水果类型。 |
| 69 | +self-value: 这个点可以采集的水果类型。 |
| 70 | +
|
| 71 | +那么对于下一个点,只需要判断: |
| 72 | +1. 是不是同类型: |
| 73 | + 同类型 repeat_count 和 count 都 + 1. |
| 74 | + 不是看2. |
| 75 | +2. 是不是在篮子里: |
| 76 | + 是则只把 count + 1 ,同时 repeat_count 和 self-value 更新为1与此点的类型。 |
| 77 | + 不是看3.。 |
| 78 | + 2.1 篮子没满,没满就 count + 1 重置 self-value repeat_count 并在 capacity 加上 这个点。 |
| 79 | +
|
| 80 | +3. 这一步是出现了不在篮子里的第三种水果类型,出现之后: |
| 81 | + count repeat_count capacity 和 self-value 全部重置。 |
| 82 | + count 为上一个点的 repeat_count + 1 |
| 83 | + repeat_count 为 1 |
| 84 | + capacity 重置为 上一个 点的 self-value + 现在的 self-value |
| 85 | + self_value 就此点的值。 |
| 86 | +
|
| 87 | +最后输出 max count即可。 |
| 88 | +
|
| 89 | +这个passed. 784ms. |
| 90 | +
|
| 91 | +测试链接: |
| 92 | +https://leetcode.com/contest/weekly-contest-102/problems/fruit-into-baskets/ |
| 93 | +
|
| 94 | +""" |
| 95 | +class Solution(object): |
| 96 | + def totalFruit(self, tree): |
| 97 | + """ |
| 98 | + :type tree: List[int] |
| 99 | + :rtype: int |
| 100 | + """ |
| 101 | + # [{count, repeat_count, capacity, self-value}, ] |
| 102 | + tree_seed = [ |
| 103 | + {'count': 1, 'repeat_count': 1, 'capacity': set([tree[0]]), 'self_value': tree[0]} |
| 104 | + ] |
| 105 | + |
| 106 | + for i in range(1, len(tree)): |
| 107 | + prev = tree_seed[i-1] |
| 108 | + |
| 109 | + # repeat self_value |
| 110 | + if tree[i] == prev.get('self_value'): |
| 111 | + tree_seed.append({'count': prev.get('count') + 1, |
| 112 | + 'repeat_count': prev.get('repeat_count') + 1, |
| 113 | + 'capacity': prev.get('capacity'), |
| 114 | + 'self_value': tree[i]}) |
| 115 | + continue |
| 116 | + |
| 117 | + # already into basket but cannot beside each other. |
| 118 | + |
| 119 | + if tree[i] in prev.get('capacity'): |
| 120 | + tree_seed.append({'count': prev.get('count') + 1, |
| 121 | + 'repeat_count': 1, |
| 122 | + 'capacity': prev.get('capacity'), |
| 123 | + 'self_value': tree[i]}) |
| 124 | + continue |
| 125 | + |
| 126 | + # != but can taken into basket. |
| 127 | + if len(tree_seed[i-1].get('capacity')) == 1: |
| 128 | + new = prev.get('capacity') |
| 129 | + new.add(tree[i]) |
| 130 | + |
| 131 | + tree_seed.append({'count': prev.get('count') + 1, |
| 132 | + 'repeat_count': 1, |
| 133 | + 'capacity': new, |
| 134 | + 'self_value': tree[i]}) |
| 135 | + continue |
| 136 | + |
| 137 | + # Found Third-fruit. |
| 138 | + new = set() |
| 139 | + new.add(tree[i]) |
| 140 | + new.add(tree[i-1]) |
| 141 | + tree_seed.append({'count': tree_seed[i-1].get('repeat_count') + 1, |
| 142 | + 'repeat_count': 1, |
| 143 | + 'capacity': new, |
| 144 | + 'self_value': tree[i]}) |
| 145 | + return max(tree_seed, key=lambda x: x['count']).get('count') |
| 146 | + |
| 147 | +# maxes = 0 |
| 148 | +# one = None |
| 149 | +# two = None |
| 150 | +# count = 0 |
| 151 | +# # for index in range(len(tree)): |
| 152 | +# index = 0 |
| 153 | +# length = len(tree) |
| 154 | + |
| 155 | +# while index < length: |
| 156 | + |
| 157 | +# i = tree[index] |
| 158 | +# if one is None: |
| 159 | +# one = i |
| 160 | +# count += 1 |
| 161 | +# index += 1 |
| 162 | +# continue |
| 163 | + |
| 164 | +# if i == one: |
| 165 | +# count += 1 |
| 166 | +# index += 1 |
| 167 | +# continue |
| 168 | + |
| 169 | +# if two is None: |
| 170 | +# two = i |
| 171 | +# count += 1 |
| 172 | +# index += 1 |
| 173 | +# continue |
| 174 | + |
| 175 | +# if i == two: |
| 176 | +# count += 1 |
| 177 | +# index += 1 |
| 178 | +# continue |
| 179 | + |
| 180 | +# maxes = max(maxes, count) |
| 181 | +# count = 1 |
| 182 | +# one = tree[index-1] |
| 183 | +# two = None |
| 184 | + |
| 185 | +# for t in range(index-2, -1, -1): |
| 186 | +# if tree[t] == one: |
| 187 | +# index = t+1 |
| 188 | +# else: |
| 189 | +# break |
| 190 | +# else: |
| 191 | +# maxes = max(maxes, count) |
| 192 | + |
| 193 | + # return maxes |
0 commit comments