Skip to content

Commit c1ad754

Browse files
committed
add 904 java
1 parent 27ad0c8 commit c1ad754

File tree

3 files changed

+140
-8
lines changed

3 files changed

+140
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@
807807
826| [Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work/) | [Python](./leetcode_python/Two_Pointers/most-profit-assigning-work.py)| _O(mlogm + nlogn)_ | _O(n)_ | Medium |`trick`, `good basic`, `zip+sorted`| OK*
808808
844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](./leetcode_python/Two_Pointers/backspace-string-compare.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/BackspaceStringCompare.java) | _O(m + n)_ | _O(1)_ | Easy |`good basic`,`stack`,`google`, `fb`| OK* (4)
809809
876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Python](./leetcode_python/Two_Pointers/middle-of-the-linked-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/MiddleOfTheLinkedList.java) | _O(n)_ | _O(1)_ | Easy |basic, amazon, apple, google, 2 pointers, linkedlist| OK* (2)
810-
904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) |[Python](./leetcode_python/Two_Pointers/fruit-into-baskets.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
810+
904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) |[Python](./leetcode_python/Two_Pointers/fruit-into-baskets.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/FruitIntoBaskets.java) | _O(n)_ | _O(1)_ | Medium |2 pointers, google| AGAIN (not start)
811811
930 | [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | [Python](./leetcode_python/Two_Pointers/binary-subarrays-with-sum.py) | _O(n)_ | _O(1)_ | Medium |`trick`| AGAIN* (2) (not start)
812812
977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](./leetcode_python/Two_Pointers/squares-of-a-sorted-array.py) | _O(n)_ | _O(1)_ | Easy || OK
813813
1055| [Shortest Way to Form String](https://leetcode.ca/2018-10-20-1055-Shortest-Way-to-Form-String/) |[Python](./leetcode_python/Two_Pointers/shortest_way_to_form_string.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/ShortestWayToFormString.java) | _O(n)_| _O(h)_ | Medium|2 pointer, `google`| again

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-03-05 -> ['279,904']
12
2025-03-04 -> ['852,692']
23
2025-03-03 -> ['247']
34
2025-03-01 -> ['399,1091']
@@ -10,6 +11,7 @@
1011
2025-02-15 -> ['369,311']
1112
2025-02-14 -> ['370']
1213
2025-02-13 -> ['815,871,593,1109']
14+
2025-02-12 -> ['279,904']
1315
2025-02-11 -> ['852,692']
1416
2025-02-10 -> ['247']
1517
2025-02-08 -> ['399,1091']
@@ -18,26 +20,26 @@
1820
2025-02-03 -> ['769,817,855(again)']
1921
2025-02-01 -> ['498(again),934', '304,853,325']
2022
2025-01-31 -> ['379,173']
21-
2025-01-30 -> ['079,212(todo),362,849']
23+
2025-01-30 -> ['279,904', '079,212(todo),362,849']
2224
2025-01-29 -> ['852,692']
2325
2025-01-28 -> ['247']
2426
2025-01-26 -> ['399,1091', '370(todo)']
2527
2025-01-25 -> ['1031,369', '369,311']
2628
2025-01-24 -> ['370', '34,767']
2729
2025-01-23 -> ['815,871,593,1109']
28-
2025-01-22 -> ['729,731']
30+
2025-01-22 -> ['279,904', '729,731']
2931
2025-01-21 -> ['852,692', '769,817,855(again)']
3032
2025-01-20 -> ['247', '722,380']
3133
2025-01-19 -> ['498(again),934', '33,81']
3234
2025-01-18 -> ['399,1091', '379,173']
33-
2025-01-17 -> ['1031,369', '079,212(todo),362,849', '560,523', '253']
35+
2025-01-17 -> ['279,904', '1031,369', '079,212(todo),362,849', '560,523', '253']
3436
2025-01-16 -> ['852,692', '776,31']
3537
2025-01-15 -> ['247', '004(todo),34(todo),162(todo),275(todo)']
36-
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
38+
2025-01-14 -> ['279,904', '729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
3739
2025-01-13 -> ['852,692', '399,1091', '769,817,855(again)']
38-
2025-01-12 -> ['247', '1031,369', '369,311']
39-
2025-01-11 -> ['852,692', '498(again),934', '370', '304,853,325', '394']
40-
2025-01-10 -> ['852,692', '247', '399,1091', '379,173', '815,871,593,1109', '833,950']
40+
2025-01-12 -> ['279,904', '247', '1031,369', '369,311']
41+
2025-01-11 -> ['279,904', '852,692', '498(again),934', '370', '304,853,325', '394']
42+
2025-01-10 -> ['279,904', '852,692', '247', '399,1091', '379,173', '815,871,593,1109', '833,950']
4143
2025-01-09 -> ['852,692', '247', '1031,369', '729,731', '079,212(todo),362,849']
4244
2025-01-08 -> ['247', '399,1091', '769,817,855(again)']
4345
2025-01-07 -> ['399,1091', '1031,369']
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package LeetCodeJava.TwoPointer;
2+
3+
// https://leetcode.com/problems/fruit-into-baskets/description/
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 904. Fruit Into Baskets
10+
* Medium
11+
* Topics
12+
* Companies
13+
* 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.
14+
*
15+
* You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
16+
*
17+
* 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.
18+
* 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.
19+
* Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
20+
* Given the integer array fruits, return the maximum number of fruits you can pick.
21+
*
22+
*
23+
*
24+
* Example 1:
25+
*
26+
* Input: fruits = [1,2,1]
27+
* Output: 3
28+
* Explanation: We can pick from all 3 trees.
29+
* Example 2:
30+
*
31+
* Input: fruits = [0,1,2,2]
32+
* Output: 3
33+
* Explanation: We can pick from trees [1,2,2].
34+
* If we had started at the first tree, we would only pick from trees [0,1].
35+
* Example 3:
36+
*
37+
* Input: fruits = [1,2,3,2,2]
38+
* Output: 4
39+
* Explanation: We can pick from trees [2,3,2,2].
40+
* If we had started at the first tree, we would only pick from trees [1,2].
41+
*
42+
*
43+
* Constraints:
44+
*
45+
* 1 <= fruits.length <= 105
46+
* 0 <= fruits[i] < fruits.length
47+
*
48+
*
49+
*/
50+
public class FruitIntoBaskets {
51+
52+
// V0
53+
// public int totalFruit(int[] fruits) {
54+
//
55+
// }
56+
57+
// V1
58+
// https://leetcode.com/problems/fruit-into-baskets/solutions/3153809/clean-codes-full-explanation-hashtable-c-dd57/
59+
public int totalFruit_1(int[] tree) {
60+
int ans = 0;
61+
Map<Integer, Integer> count = new HashMap<>();
62+
63+
for (int l = 0, r = 0; r < tree.length; ++r) {
64+
count.put(tree[r], count.getOrDefault(tree[r], 0) + 1);
65+
while (count.size() > 2) {
66+
count.put(tree[l], count.get(tree[l]) - 1);
67+
count.remove(tree[l], 0);
68+
++l;
69+
}
70+
ans = Math.max(ans, r - l + 1);
71+
}
72+
73+
return ans;
74+
}
75+
76+
// V2
77+
// https://leetcode.com/problems/fruit-into-baskets/solutions/3153492/easy-explanation-with-animation-and-vide-mo3t/
78+
public int totalFruit_2(int[] fruits) {
79+
Map<Integer, Integer> basket = new HashMap<>();
80+
int j = 0;
81+
int i = 0;
82+
int res = 0;
83+
for (i = 0; i < fruits.length; i++) {
84+
basket.put(fruits[i], basket.getOrDefault(fruits[i], 0) + 1);
85+
while (basket.size() > 2) {
86+
basket.put(fruits[j], basket.get(fruits[j]) - 1);
87+
basket.remove(fruits[j], 0);
88+
j++;
89+
}
90+
res = Math.max(res, i - j + 1);
91+
}
92+
return res;
93+
}
94+
95+
// V3
96+
// https://leetcode.com/problems/fruit-into-baskets/solutions/3153971/crystal-clear-explanation-c-java-by-myst-73x0/
97+
public int totalFruit_3(int[] fruits) {
98+
// Initializing a map for keeping count of distinct fruits
99+
Map<Integer, Integer> fruitCount = new HashMap<>();
100+
101+
// i is front pointer of sliding window
102+
// j is rear pointer of sliding window
103+
// ans will store the maximum subarray length
104+
int i = 0, j = 0, ans = 0;
105+
while (i < fruits.length) {
106+
// picking up the fruit
107+
fruitCount.put(fruits[i], fruitCount.getOrDefault(fruits[i], 0) + 1);
108+
// if no. of distinct fruits is more than two
109+
// we will move our rear pointer and unpick the fruits
110+
while (fruitCount.size() > 2) {
111+
int count = fruitCount.get(fruits[j]) - 1;
112+
fruitCount.put(fruits[j], count);
113+
// if fuit of a particular type are exhausted
114+
// clearing the index of that fruit
115+
if (count == 0) {
116+
fruitCount.remove(fruits[j]);
117+
}
118+
// moving rear pointer
119+
j++;
120+
}
121+
// Storing maximum subarray length with at most 2 distinct fruits
122+
ans = Math.max(ans, i - j + 1);
123+
// moving front pointer
124+
i++;
125+
}
126+
// returning the maximum subarray length
127+
return ans;
128+
}
129+
130+
}

0 commit comments

Comments
 (0)