Skip to content

Commit 353828a

Browse files
authored
feat: add solutions to lc problems: No.0904,1695 (#2573)
* No.0904.Fruit Into Baskets * No.1695.Maximum Erasure Value
1 parent c2699ed commit 353828a

17 files changed

+472
-177
lines changed

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

+62-59
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ class Solution {
119119
int ans = 0;
120120
for (int i = 0, j = 0; i < fruits.length; ++i) {
121121
int x = fruits[i];
122-
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
122+
cnt.merge(x, 1, Integer::sum);
123123
while (cnt.size() > 2) {
124124
int y = fruits[j++];
125-
cnt.put(y, cnt.get(y) - 1);
126-
if (cnt.get(y) == 0) {
125+
if (cnt.merge(y, -1, Integer::sum) == 0) {
127126
cnt.remove(y);
128127
}
129128
}
@@ -145,7 +144,9 @@ public:
145144
++cnt[x];
146145
while (cnt.size() > 2) {
147146
int y = fruits[j++];
148-
if (--cnt[y] == 0) cnt.erase(y);
147+
if (--cnt[y] == 0) {
148+
cnt.erase(y);
149+
}
149150
}
150151
ans = max(ans, i - j + 1);
151152
}
@@ -176,49 +177,47 @@ func totalFruit(fruits []int) int {
176177
```ts
177178
function totalFruit(fruits: number[]): number {
178179
const n = fruits.length;
179-
const map = new Map<number, number>();
180-
let res = 0;
181-
let left = 0;
182-
let right = 0;
183-
while (right < n) {
184-
map.set(fruits[right], (map.get(fruits[right]) ?? 0) + 1);
185-
right++;
186-
while (map.size > 2) {
187-
const k = fruits[left++];
188-
map.set(k, map.get(k) - 1);
189-
if (map.get(k) === 0) {
190-
map.delete(k);
180+
const cnt: Map<number, number> = new Map();
181+
let ans = 0;
182+
for (let i = 0, j = 0; i < n; ++i) {
183+
cnt.set(fruits[i], (cnt.get(fruits[i]) || 0) + 1);
184+
for (; cnt.size > 2; ++j) {
185+
cnt.set(fruits[j], cnt.get(fruits[j])! - 1);
186+
if (!cnt.get(fruits[j])) {
187+
cnt.delete(fruits[j]);
191188
}
192189
}
193-
res = Math.max(res, right - left);
190+
ans = Math.max(ans, i - j + 1);
194191
}
195-
return res;
192+
return ans;
196193
}
197194
```
198195

199196
```rust
200197
use std::collections::HashMap;
198+
201199
impl Solution {
202200
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
203-
let n = fruits.len();
204-
let mut map = HashMap::new();
205-
let mut res = 0;
206-
let mut left = 0;
207-
let mut right = 0;
208-
while right < n {
209-
*map.entry(fruits[right]).or_insert(0) += 1;
210-
right += 1;
211-
while map.len() > 2 {
212-
let k = fruits[left];
213-
map.insert(k, map[&k] - 1);
214-
if map[&k] == 0 {
215-
map.remove(&k);
201+
let mut cnt = HashMap::new();
202+
let mut ans = 0;
203+
let mut j = 0;
204+
205+
for (i, &x) in fruits.iter().enumerate() {
206+
*cnt.entry(x).or_insert(0) += 1;
207+
208+
while cnt.len() > 2 {
209+
let y = fruits[j];
210+
j += 1;
211+
*cnt.get_mut(&y).unwrap() -= 1;
212+
if cnt[&y] == 0 {
213+
cnt.remove(&y);
216214
}
217-
left += 1;
218215
}
219-
res = res.max(right - left);
216+
217+
ans = ans.max(i - j + 1);
220218
}
221-
res as i32
219+
220+
ans as i32
222221
}
223222
}
224223
```
@@ -257,11 +256,10 @@ class Solution {
257256
Map<Integer, Integer> cnt = new HashMap<>();
258257
int j = 0, n = fruits.length;
259258
for (int x : fruits) {
260-
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
259+
cnt.merge(x, 1, Integer::sum);
261260
if (cnt.size() > 2) {
262261
int y = fruits[j++];
263-
cnt.put(y, cnt.get(y) - 1);
264-
if (cnt.get(y) == 0) {
262+
if (cnt.merge(y, -1, Integer::sum) == 0) {
265263
cnt.remove(y);
266264
}
267265
}
@@ -281,7 +279,9 @@ public:
281279
++cnt[x];
282280
if (cnt.size() > 2) {
283281
int y = fruits[j++];
284-
if (--cnt[y] == 0) cnt.erase(y);
282+
if (--cnt[y] == 0) {
283+
cnt.erase(y);
284+
}
285285
}
286286
}
287287
return n - j;
@@ -311,41 +311,44 @@ func totalFruit(fruits []int) int {
311311
```ts
312312
function totalFruit(fruits: number[]): number {
313313
const n = fruits.length;
314-
const map = new Map<number, number>();
315-
let i = 0;
316-
for (const fruit of fruits) {
317-
map.set(fruit, (map.get(fruit) ?? 0) + 1);
318-
if (map.size > 2) {
319-
const k = fruits[i++];
320-
map.set(k, map.get(k) - 1);
321-
if (map.get(k) == 0) {
322-
map.delete(k);
314+
const cnt: Map<number, number> = new Map();
315+
let j = 0;
316+
for (const x of fruits) {
317+
cnt.set(x, (cnt.get(x) || 0) + 1);
318+
if (cnt.size > 2) {
319+
cnt.set(fruits[j], cnt.get(fruits[j])! - 1);
320+
if (!cnt.get(fruits[j])) {
321+
cnt.delete(fruits[j]);
323322
}
323+
++j;
324324
}
325325
}
326-
return n - i;
326+
return n - j;
327327
}
328328
```
329329

330330
```rust
331331
use std::collections::HashMap;
332+
332333
impl Solution {
333334
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
335+
let mut cnt = HashMap::new();
336+
let mut j = 0;
334337
let n = fruits.len();
335-
let mut map = HashMap::new();
336-
let mut i = 0;
337-
for &fruit in fruits.iter() {
338-
*map.entry(fruit).or_insert(0) += 1;
339-
if map.len() > 2 {
340-
let k = fruits[i];
341-
map.insert(k, map[&k] - 1);
342-
if map[&k] == 0 {
343-
map.remove(&k);
338+
339+
for &x in &fruits {
340+
*cnt.entry(x).or_insert(0) += 1;
341+
if cnt.len() > 2 {
342+
let y = fruits[j];
343+
j += 1;
344+
*cnt.get_mut(&y).unwrap() -= 1;
345+
if cnt[&y] == 0 {
346+
cnt.remove(&y);
344347
}
345-
i += 1;
346348
}
347349
}
348-
(n - i) as i32
350+
351+
(n - j) as i32
349352
}
350353
}
351354
```

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

+62-59
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ class Solution {
107107
int ans = 0;
108108
for (int i = 0, j = 0; i < fruits.length; ++i) {
109109
int x = fruits[i];
110-
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
110+
cnt.merge(x, 1, Integer::sum);
111111
while (cnt.size() > 2) {
112112
int y = fruits[j++];
113-
cnt.put(y, cnt.get(y) - 1);
114-
if (cnt.get(y) == 0) {
113+
if (cnt.merge(y, -1, Integer::sum) == 0) {
115114
cnt.remove(y);
116115
}
117116
}
@@ -133,7 +132,9 @@ public:
133132
++cnt[x];
134133
while (cnt.size() > 2) {
135134
int y = fruits[j++];
136-
if (--cnt[y] == 0) cnt.erase(y);
135+
if (--cnt[y] == 0) {
136+
cnt.erase(y);
137+
}
137138
}
138139
ans = max(ans, i - j + 1);
139140
}
@@ -164,49 +165,47 @@ func totalFruit(fruits []int) int {
164165
```ts
165166
function totalFruit(fruits: number[]): number {
166167
const n = fruits.length;
167-
const map = new Map<number, number>();
168-
let res = 0;
169-
let left = 0;
170-
let right = 0;
171-
while (right < n) {
172-
map.set(fruits[right], (map.get(fruits[right]) ?? 0) + 1);
173-
right++;
174-
while (map.size > 2) {
175-
const k = fruits[left++];
176-
map.set(k, map.get(k) - 1);
177-
if (map.get(k) === 0) {
178-
map.delete(k);
168+
const cnt: Map<number, number> = new Map();
169+
let ans = 0;
170+
for (let i = 0, j = 0; i < n; ++i) {
171+
cnt.set(fruits[i], (cnt.get(fruits[i]) || 0) + 1);
172+
for (; cnt.size > 2; ++j) {
173+
cnt.set(fruits[j], cnt.get(fruits[j])! - 1);
174+
if (!cnt.get(fruits[j])) {
175+
cnt.delete(fruits[j]);
179176
}
180177
}
181-
res = Math.max(res, right - left);
178+
ans = Math.max(ans, i - j + 1);
182179
}
183-
return res;
180+
return ans;
184181
}
185182
```
186183

187184
```rust
188185
use std::collections::HashMap;
186+
189187
impl Solution {
190188
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
191-
let n = fruits.len();
192-
let mut map = HashMap::new();
193-
let mut res = 0;
194-
let mut left = 0;
195-
let mut right = 0;
196-
while right < n {
197-
*map.entry(fruits[right]).or_insert(0) += 1;
198-
right += 1;
199-
while map.len() > 2 {
200-
let k = fruits[left];
201-
map.insert(k, map[&k] - 1);
202-
if map[&k] == 0 {
203-
map.remove(&k);
189+
let mut cnt = HashMap::new();
190+
let mut ans = 0;
191+
let mut j = 0;
192+
193+
for (i, &x) in fruits.iter().enumerate() {
194+
*cnt.entry(x).or_insert(0) += 1;
195+
196+
while cnt.len() > 2 {
197+
let y = fruits[j];
198+
j += 1;
199+
*cnt.get_mut(&y).unwrap() -= 1;
200+
if cnt[&y] == 0 {
201+
cnt.remove(&y);
204202
}
205-
left += 1;
206203
}
207-
res = res.max(right - left);
204+
205+
ans = ans.max(i - j + 1);
208206
}
209-
res as i32
207+
208+
ans as i32
210209
}
211210
}
212211
```
@@ -245,11 +244,10 @@ class Solution {
245244
Map<Integer, Integer> cnt = new HashMap<>();
246245
int j = 0, n = fruits.length;
247246
for (int x : fruits) {
248-
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
247+
cnt.merge(x, 1, Integer::sum);
249248
if (cnt.size() > 2) {
250249
int y = fruits[j++];
251-
cnt.put(y, cnt.get(y) - 1);
252-
if (cnt.get(y) == 0) {
250+
if (cnt.merge(y, -1, Integer::sum) == 0) {
253251
cnt.remove(y);
254252
}
255253
}
@@ -269,7 +267,9 @@ public:
269267
++cnt[x];
270268
if (cnt.size() > 2) {
271269
int y = fruits[j++];
272-
if (--cnt[y] == 0) cnt.erase(y);
270+
if (--cnt[y] == 0) {
271+
cnt.erase(y);
272+
}
273273
}
274274
}
275275
return n - j;
@@ -299,41 +299,44 @@ func totalFruit(fruits []int) int {
299299
```ts
300300
function totalFruit(fruits: number[]): number {
301301
const n = fruits.length;
302-
const map = new Map<number, number>();
303-
let i = 0;
304-
for (const fruit of fruits) {
305-
map.set(fruit, (map.get(fruit) ?? 0) + 1);
306-
if (map.size > 2) {
307-
const k = fruits[i++];
308-
map.set(k, map.get(k) - 1);
309-
if (map.get(k) == 0) {
310-
map.delete(k);
302+
const cnt: Map<number, number> = new Map();
303+
let j = 0;
304+
for (const x of fruits) {
305+
cnt.set(x, (cnt.get(x) || 0) + 1);
306+
if (cnt.size > 2) {
307+
cnt.set(fruits[j], cnt.get(fruits[j])! - 1);
308+
if (!cnt.get(fruits[j])) {
309+
cnt.delete(fruits[j]);
311310
}
311+
++j;
312312
}
313313
}
314-
return n - i;
314+
return n - j;
315315
}
316316
```
317317

318318
```rust
319319
use std::collections::HashMap;
320+
320321
impl Solution {
321322
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
323+
let mut cnt = HashMap::new();
324+
let mut j = 0;
322325
let n = fruits.len();
323-
let mut map = HashMap::new();
324-
let mut i = 0;
325-
for &fruit in fruits.iter() {
326-
*map.entry(fruit).or_insert(0) += 1;
327-
if map.len() > 2 {
328-
let k = fruits[i];
329-
map.insert(k, map[&k] - 1);
330-
if map[&k] == 0 {
331-
map.remove(&k);
326+
327+
for &x in &fruits {
328+
*cnt.entry(x).or_insert(0) += 1;
329+
if cnt.len() > 2 {
330+
let y = fruits[j];
331+
j += 1;
332+
*cnt.get_mut(&y).unwrap() -= 1;
333+
if cnt[&y] == 0 {
334+
cnt.remove(&y);
332335
}
333-
i += 1;
334336
}
335337
}
336-
(n - i) as i32
338+
339+
(n - j) as i32
337340
}
338341
}
339342
```

solution/0900-0999/0904.Fruit Into Baskets/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class Solution {
88
++cnt[x];
99
while (cnt.size() > 2) {
1010
int y = fruits[j++];
11-
if (--cnt[y] == 0) cnt.erase(y);
11+
if (--cnt[y] == 0) {
12+
cnt.erase(y);
13+
}
1214
}
1315
ans = max(ans, i - j + 1);
1416
}

0 commit comments

Comments
 (0)