904. Fruit into Baskets Leetcode Solution
https://leetcode.com/problems/fruit-into-baskets/
In a row of trees, the
i
-th tree produces fruit with type tree[i]
.You start at any tree of your choice, then repeatedly perform the following steps:
- Add one piece of fruit from this tree to your baskets. If you cannot, stop.
- Move to the next tree to the right of the current tree. If there is no tree to the right, stop.
Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.
You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.
What is the total amount of fruit you can collect with this procedure?
Example 1:
Input: [1,2,1] Output: 3 Explanation: We can collect [1,2,1].
Example 2:
Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first tree, we would only collect [0, 1].
Example 3:
Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first tree, we would only collect [1, 2].
Example 4:
Input: [3,3,3,1,2,1,1,2,3,3,4] Output: 5 Explanation: We can collect [1,2,1,1,2]. If we started at the first tree or the eighth tree, we would only collect 4 fruits.
WalkThrough:
Suppose we have a row of trees, the i-th tree produces fruit with type tree[i]. we can start at any tree of our choice, then repeatedly perform these steps −
- Add one piece of fruit from this tree to our baskets. If there is no chance, then stop.
- Move to the next tree to the right of the current one. If there is no tree to the right, then stop.
We have two baskets, and each basket can carry any quantity of fruit, but we want each basket should only carry one type of fruit each. We have to find the total amount of fruit that we can collect with this procedure? So if the trees are like [0, 1, 2, 2], then the output will be 3. We can collect [1,2,2], if we start from the first tree, then we would only collect [0, 1]
To solve this, we will follow these steps −
- n := tree size, j := 0, ans := 0
- create one map m
- for i in range 0 to n – 1
- increase m[tree[i]] by 1
- while size of m is > 2 and j <= i, then
- decrease m[tree[j]] by 1
- if m[tree[j]] = 0, then delete tree[j] from m
- increase j by 1
- ans := max of i – j + 1 and ans
- return ans
#include <bits/stdc++.h> using namespace std; class Solution { public: int totalFruit(vector<int>& tree) { int n = tree.size(); int j = 0; map <int, int> m; int ans = 0; for(int i = 0; i < n; i++){ m[tree[i]] += 1; while(m.size() > 2 && j <= i){ m[tree[j]]--; if(m[tree[j]] == 0)m.erase(tree[j]); j++; } ans = max(i - j + 1, ans); } return ans; } }; main(){ vector<int> v = {3,3,3,1,2,1,1,2,3,3,4}; Solution ob; cout <<(ob.totalFruit(v)); }
Input
[3,3,3,1,2,1,1,2,3,3,4]
Output
5
This is essentially the same question as: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ . Just imagine each number as a character
https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window
https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window
"Start from any index, we can collect at most two types of fruits. What is the maximum amount"
public int totalFruit(int[] tree) {
Map<Integer, Integer> count = new HashMap<Integer, Integer>();
int res = 0, i = 0;
for (int j = 0; j < tree.length; ++j) {
count.put(tree[j], count.getOrDefault(tree[j], 0) + 1);
while (count.size() > 2) {
count.put(tree[i], count.get(tree[i]) - 1);
if (count.get(tree[i]) == 0) count.remove(tree[i]);
i++;
}
res = Math.max(res, j - i + 1);
}
return res;
}