Skip to content

Commit 750c086

Browse files
committed
feat: add solutions to lc problem: No.0904. Fruit Into Baskets
1 parent 1aaa25e commit 750c086

File tree

5 files changed

+96
-5
lines changed

5 files changed

+96
-5
lines changed

solution/0900-0999/0904.Fruit Into Baskets/README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,53 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
“计数器 + 滑动窗口”实现。
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

7476
<!-- 这里可写当前语言的特殊实现逻辑 -->
7577

7678
```python
77-
79+
class Solution:
80+
def totalFruit(self, tree: List[int]) -> int:
81+
counter = collections.Counter()
82+
i = res = 0
83+
for j, type in enumerate(tree):
84+
counter[type] += 1
85+
while len(counter) > 2:
86+
counter[tree[i]] -= 1
87+
if counter[tree[i]] == 0:
88+
counter.pop(tree[i])
89+
i += 1
90+
res = max(res, j - i + 1)
91+
return res
7892
```
7993

8094
### **Java**
8195

8296
<!-- 这里可写当前语言的特殊实现逻辑 -->
8397

8498
```java
85-
99+
class Solution {
100+
public int totalFruit(int[] tree) {
101+
Map<Integer, Integer> counter = new HashMap<>();
102+
int i = 0, res = 0;
103+
for (int j = 0; j < tree.length; ++j) {
104+
counter.put(tree[j], counter.getOrDefault(tree[j], 0) + 1);
105+
while (counter.size() > 2) {
106+
counter.put(tree[i], counter.get(tree[i]) - 1);
107+
if (counter.get(tree[i]) == 0) {
108+
counter.remove(tree[i]);
109+
}
110+
++i;
111+
}
112+
res = Math.max(res, j - i + 1);
113+
}
114+
return res;
115+
}
116+
}
86117
```
87118

88119
### **...**

solution/0900-0999/0904.Fruit Into Baskets/README_EN.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,42 @@ If we started at the first tree, we would only collect [0, 1].</span>
139139
### **Python3**
140140

141141
```python
142-
142+
class Solution:
143+
def totalFruit(self, tree: List[int]) -> int:
144+
counter = collections.Counter()
145+
i = res = 0
146+
for j, type in enumerate(tree):
147+
counter[type] += 1
148+
while len(counter) > 2:
149+
counter[tree[i]] -= 1
150+
if counter[tree[i]] == 0:
151+
counter.pop(tree[i])
152+
i += 1
153+
res = max(res, j - i + 1)
154+
return res
143155
```
144156

145157
### **Java**
146158

147159
```java
148-
160+
class Solution {
161+
public int totalFruit(int[] tree) {
162+
Map<Integer, Integer> counter = new HashMap<>();
163+
int i = 0, res = 0;
164+
for (int j = 0; j < tree.length; ++j) {
165+
counter.put(tree[j], counter.getOrDefault(tree[j], 0) + 1);
166+
while (counter.size() > 2) {
167+
counter.put(tree[i], counter.get(tree[i]) - 1);
168+
if (counter.get(tree[i]) == 0) {
169+
counter.remove(tree[i]);
170+
}
171+
++i;
172+
}
173+
res = Math.max(res, j - i + 1);
174+
}
175+
return res;
176+
}
177+
}
149178
```
150179

151180
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int totalFruit(int[] tree) {
3+
Map<Integer, Integer> counter = new HashMap<>();
4+
int i = 0, res = 0;
5+
for (int j = 0; j < tree.length; ++j) {
6+
counter.put(tree[j], counter.getOrDefault(tree[j], 0) + 1);
7+
while (counter.size() > 2) {
8+
counter.put(tree[i], counter.get(tree[i]) - 1);
9+
if (counter.get(tree[i]) == 0) {
10+
counter.remove(tree[i]);
11+
}
12+
++i;
13+
}
14+
res = Math.max(res, j - i + 1);
15+
}
16+
return res;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def totalFruit(self, tree: List[int]) -> int:
3+
counter = collections.Counter()
4+
i = res = 0
5+
for j, type in enumerate(tree):
6+
counter[type] += 1
7+
while len(counter) > 2:
8+
counter[tree[i]] -= 1
9+
if counter[tree[i]] == 0:
10+
counter.pop(tree[i])
11+
i += 1
12+
res = max(res, j - i + 1)
13+
return res

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
二分法。
7070

71-
以“二分法”方式枚举速度值,找到满足条件的最小速度。
71+
以“二分”的方式枚举速度值,找到满足条件的最小速度。
7272

7373
<!-- tabs:start -->
7474

0 commit comments

Comments
 (0)