@@ -226,7 +226,41 @@ class Solution:
226
226
else : return res
227
227
```
228
228
229
+ ### [ 水果成篮] ( https://leetcode.cn/problems/fruit-into-baskets/ )
230
+ > 你正在探访一家农场,农场从左到右种植了一排果树。这些树用一个整数数组 fruits 表示,其中 fruits[ i] 是第 i 棵树上的水果 种类 。
231
+ >
232
+ > 你想要尽可能多地收集水果。然而,农场的主人设定了一些严格的规矩,你必须按照要求采摘水果:
233
+ >
234
+ > 你只有 两个 篮子,并且每个篮子只能装 单一类型 的水果。每个篮子能够装的水果总量没有限制。
235
+ 你可以选择任意一棵树开始采摘,你必须从 每棵 树(包括开始采摘的树)上 恰好摘一个水果 。采摘的水果应当符合篮子中的水果类型。每采摘一次,你将会向右移动到下一棵树,并继续采摘。
236
+ 一旦你走到某棵树前,但水果不符合篮子的水果类型,那么就必须停止采摘。
237
+ 给你一个整数数组 fruits ,返回你可以收集的水果的 ** 最大** 数目。
229
238
239
+ 思路:跟上面的长度最小子数组类似,本题则是求解长度最大子数组
240
+ ** Python版本**
241
+ ``` python
242
+ from collections import Counter
243
+ class Solution :
244
+ def totalFruit (self , fruits : List[int ]) -> int :
245
+ '''
246
+ 滑动窗口,窗口内子数组满足只有两个unique数字,寻找最大长度窗口.
247
+ '''
248
+ res = 0
249
+ start = 0
250
+ window = Counter()
251
+ for end in range (len (fruits)):
252
+ num_e = fruits[end]
253
+ window[num_e] += 1
254
+ while len (window) > 2 and start <= end:
255
+ num_s = fruits[start]
256
+ window[num_s] -= 1
257
+ if window[num_s] == 0 : window.pop(num_s)
258
+ start = start + 1
259
+ subL = end - start + 1
260
+ res = max (res, subL)
261
+
262
+ return res
263
+ ```
230
264
231
265
232
266
### [ spiral-matrix] ( https://leetcode-cn.com/problems/spiral-matrix/ )
0 commit comments