Skip to content

Commit 2731348

Browse files
committed
leetcode
1 parent 9f91bc9 commit 2731348

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
3+
4+
-* 904. Fruit Into Baskets *-
5+
6+
7+
8+
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.
9+
10+
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
11+
12+
You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
13+
Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
14+
Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
15+
Given the integer array fruits, return the maximum number of fruits you can pick.
16+
17+
18+
19+
Example 1:
20+
21+
Input: fruits = [1,2,1]
22+
Output: 3
23+
Explanation: We can pick from all 3 trees.
24+
Example 2:
25+
26+
Input: fruits = [0,1,2,2]
27+
Output: 3
28+
Explanation: We can pick from trees [1,2,2].
29+
If we had started at the first tree, we would only pick from trees [0,1].
30+
Example 3:
31+
32+
Input: fruits = [1,2,3,2,2]
33+
Output: 4
34+
Explanation: We can pick from trees [2,3,2,2].
35+
If we had started at the first tree, we would only pick from trees [1,2].
36+
37+
38+
Constraints:
39+
40+
1 <= fruits.length <= 105
41+
0 <= fruits[i] < fruits.length
42+
43+
44+
*/
45+
46+
import 'dart:collection';
47+
import 'dart:math';
48+
49+
class A {
50+
int totalFruit(List<int> fruits) {
51+
List<int> counts = List.filled(fruits.length, 0);
52+
int maximum = 0;
53+
int currentMax = 0;
54+
int typeCount = 0;
55+
int start = 0;
56+
57+
for (int i = 0; i < fruits.length; i++) {
58+
if (counts[fruits[i]] == 0) typeCount++;
59+
counts[fruits[i]]++;
60+
currentMax++;
61+
62+
while (typeCount > 2 && start < i) {
63+
counts[fruits[start]]--;
64+
if (counts[fruits[start]] == 0) typeCount--;
65+
start++;
66+
currentMax--;
67+
}
68+
69+
maximum = max(maximum, currentMax);
70+
}
71+
72+
return maximum;
73+
}
74+
}
75+
76+
class B {
77+
int totalFruit(List<int> fruits) {
78+
HashMap<int, int> basket = HashMap();
79+
int j = 0;
80+
int i = 0;
81+
int res = 0;
82+
for (i = 0; i < fruits.length; i++) {
83+
basket[fruits[i]] = (basket[fruits[i]] ?? 0) + 1;
84+
while (basket.length > 2) {
85+
basket[fruits[j]] = basket[fruits[j]]! - 1;
86+
basket.remove(fruits[j]);
87+
j++;
88+
}
89+
res = max(res, i - j + 1);
90+
}
91+
return res;
92+
}
93+
}
94+
95+
class C {
96+
int totalFruit(List<int> fruits) {
97+
int n = fruits.length, ans = 0;
98+
// count the frequency of fruits
99+
HashMap<int, int> m = HashMap();
100+
// two pointers
101+
// - l is the pointer to the starting index of the window
102+
// - r is the pointer to the ending index of the window
103+
for (int l = 0, r = 0; r < n; r++) {
104+
// add fruits[r] to a hashmap
105+
m[fruits[r]] = (m[fruits[r]] ?? 0) + 1;
106+
// if there is more than two types
107+
if (m.length > 2) {
108+
// then we need to sub-tract one from the freq of leftmost element, i.e. fruits[l]
109+
// if it is 0, then we can erase it
110+
m[fruits[l]] = (m[fruits[l]] ?? 0) - 1;
111+
if (m[fruits[l]] == 0) m.remove(fruits[l]);
112+
// shrink the window by moving the `l` to the right
113+
l += 1;
114+
}
115+
// the maximum number of fruits we can pick is simply the window size
116+
ans = max(ans, r - l + 1);
117+
}
118+
return ans;
119+
}
120+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
func totalFruit(f []int) int {
4+
if len(f) == 0 {
5+
return 0
6+
}
7+
8+
if len(f) == 1 {
9+
return 1
10+
}
11+
12+
res := -1 << 63
13+
14+
m := make(map[int]int)
15+
16+
l := 0
17+
m[f[l]]++
18+
19+
for i := 1; i < len(f); i++ {
20+
m[f[i]]++
21+
22+
for len(m) > 2 {
23+
m[f[l]]--
24+
25+
if m[f[l]] == 0 {
26+
delete(m, f[l])
27+
}
28+
29+
l++
30+
}
31+
32+
if i-l+1 > res {
33+
res = i - l + 1
34+
}
35+
}
36+
37+
return res
38+
}
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 🔥 100% Fast 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Approach
4+
5+
Initialize an array counts to store the count of each type of fruit.
6+
Initialize variables max to store the maximum length of the sub-array, currentMax to store the current length of the sub-array, typeCount to store the number of different types of fruit in the sub-array, and start to store the start index of the sub-array.
7+
Iterate through the fruits array and increment the count of the current fruit in counts. Also, increment currentMax and typeCount if the current fruit is a new type.
8+
While the number of different fruit types in the sub-array exceeds 2, and start is less than i, decrement the count of the fruit at start in counts, decrement currentMax, and increment start. If the count of the fruit becomes 0, decrement typeCount.
9+
Update max with the maximum value of max and currentMax.
10+
Return max as the result.
11+
12+
## Complexity
13+
14+
- Time complexity: O(n)
15+
This is because the solution uses a single loop through the fruits
16+
17+
- Space complexity: O(n)
18+
This is because the solution uses an array counts of size n to store the count of each fruit type
19+
20+
## Code - 1
21+
22+
```dart
23+
class Solution {
24+
int totalFruit(List<int> fruits) {
25+
List<int> counts = List.filled(fruits.length, 0);
26+
int maximum = 0;
27+
int currentMax = 0;
28+
int typeCount = 0;
29+
int start = 0;
30+
31+
for (int i = 0; i < fruits.length; i++) {
32+
if (counts[fruits[i]] == 0) typeCount++;
33+
counts[fruits[i]]++;
34+
currentMax++;
35+
36+
while (typeCount > 2 && start < i) {
37+
counts[fruits[start]]--;
38+
if (counts[fruits[start]] == 0) typeCount--;
39+
start++;
40+
currentMax--;
41+
}
42+
43+
maximum = max(maximum, currentMax);
44+
}
45+
46+
return maximum;
47+
}
48+
}
49+
```
50+
51+
## Code - 2
52+
53+
```dart
54+
class Solution {
55+
int totalFruit(List<int> fruits) {
56+
int n = fruits.length, ans = 0;
57+
// count the frequency of fruits
58+
HashMap<int, int> m = HashMap();
59+
// two pointers
60+
// - l is the pointer to the starting index of the window
61+
// - r is the pointer to the ending index of the window
62+
for (int l = 0, r = 0; r < n; r++) {
63+
// add fruits[r] to a hashmap
64+
m[fruits[r]] = (m[fruits[r]] ?? 0) + 1;
65+
// if there is more than two types
66+
if (m.length > 2) {
67+
// then we need to sub-tract one from the freq of leftmost element, i.e. fruits[l]
68+
// if it is 0, then we can erase it
69+
m[fruits[l]] = (m[fruits[l]] ?? 0) - 1;
70+
if (m[fruits[l]] == 0) m.remove(fruits[l]);
71+
// shrink the window by moving the `l` to the right
72+
l += 1;
73+
}
74+
// the maximum number of fruits we can pick is simply the window size
75+
ans = max(ans, r - l + 1);
76+
}
77+
return ans;
78+
}
79+
}
80+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
204204
- [**953.** Verifying an Alien Dictionary](VerifyingAnAlienDictionary/verifying_an_alien_dictionary.dart)
205205
- [**6.** Zigzag Conversion](ZigzagConversion/zigzag_conversion.dart)
206206
- [**1470.** Shuffle the Array](ShuffleTheArray/shuffle_the_array.dart)
207+
- [**904.** Fruit Into Baskets](FruitIntoBaskets/fruit_into_baskets.dart)
207208

208209
## Reach me via
209210

0 commit comments

Comments
 (0)