-
Notifications
You must be signed in to change notification settings - Fork 1
/
904.fruit-into-baskets.java
103 lines (99 loc) · 2.46 KB
/
904.fruit-into-baskets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* @lc app=leetcode id=904 lang=java
*
* [904] Fruit Into Baskets
*
* https://leetcode.com/problems/fruit-into-baskets/description/
*
* algorithms
* Medium (43.73%)
* Likes: 3807
* Dislikes: 275
* Total Accepted: 330.9K
* Total Submissions: 757.1K
* Testcase Example: '[1,2,1]'
*
* 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 i^th tree produces.
*
* You want to collect as much fruit as possible. However, the owner has some
* strict rules that you must follow:
*
*
* 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.
* 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.
* Once you reach a tree with fruit that cannot fit in your baskets, you must
* stop.
*
*
* Given the integer array fruits, return the maximum number of fruits you can
* pick.
*
*
* Example 1:
*
*
* Input: fruits = [1,2,1]
* Output: 3
* Explanation: We can pick from all 3 trees.
*
*
* Example 2:
*
*
* Input: fruits = [0,1,2,2]
* Output: 3
* Explanation: We can pick from trees [1,2,2].
* If we had started at the first tree, we would only pick from trees [0,1].
*
*
* Example 3:
*
*
* Input: fruits = [1,2,3,2,2]
* Output: 4
* Explanation: We can pick from trees [2,3,2,2].
* If we had started at the first tree, we would only pick from trees
* [1,2].
*
*
*
* Constraints:
*
*
* 1 <= fruits.length <= 10^5
* 0 <= fruits[i] < fruits.length
*
*
*/
// @lc code=start
class Solution {
public int totalFruit(int[] fruits) {
int max = 0;
int left = 0;
int right = 0;
int count = 0;
int[] basket = new int[fruits.length];
while (right < fruits.length) {
if (basket[fruits[right]] == 0) {
count++;
}
basket[fruits[right]]++;
right++;
while (count > 2) {
basket[fruits[left]]--;
if (basket[fruits[left]] == 0) {
count--;
}
left++;
}
max = Math.max(max, right - left);
}
return max;
}
}
// @lc code=end