-
Notifications
You must be signed in to change notification settings - Fork 0
/
0904.FruitIntoBaskets.js
129 lines (100 loc) · 3.91 KB
/
0904.FruitIntoBaskets.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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.
// 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].
// Example 4:
// Input: fruits = [3,3,3,1,2,1,1,2,3,3,4]
// Output: 5
// Explanation: We can pick from trees [1,2,1,1,2].
//
// Constraints:
// 1 <= fruits.length <= 105
// 0 <= fruits[i] < fruits.length
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/fruit-into-baskets
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 🔥🔥🔥 翻译:求只包含两种元素的最长连续子序列
// 🎨 方法一:滑动窗口 - 有问题
/**
* @param {number[]} fruits
* @return {number}
*/
var totalFruit = function (fruits) {
let len = fruits.length;
if (len <= 2) {
return len
}
let ans = 0;
// 记录窗口map 种类 => 出现次数
let map = new Map();
map.set(fruits[0], (map.get(fruits[0]) || 0) + 1)
map.set(fruits[1], (map.get(fruits[1]) || 0) + 1);
// 滑动窗口的右侧指针
let end = 2
// 滑动窗口左侧指针
let start = 0
while (end < len) {
map.set(fruits[end], (map.get(fruits[end]) || 0) + 1);
while (uniSize(map) > 2) {
map.set(fruits[start], map.get(fruits[start]) - 1);
start++
}
ans = Math.max(ans, end - start + 1);
end++
}
return ans
};
function uniSize(map) {
let set = new Set();
for (let [fruit, count] of map) {
if (count > 0) {
set.add(fruit)
}
}
return set.size
}
// 🎨 方法一:滑动窗口 + map - 优化
// 📝 思路:构建区间 [left,right] 的窗口,并且用map记录窗口中下标标对应元素出现的次数,right 遍历数组时,更新追加right对应元素的map中出现的次数,不满足条件则右移left直到满足,过程后中不断更新 res
// 😁 类似:https://github.com/shanejix/algorithm-and-data-structure/blob/5219f60e19fbcf616aa84885583402bf8f5d4a39/pattern/sliding-window/0003.LongestSubstringWithoutRepeatingCharacters/solution.js
/**
* @param {number[]} fruits
* @return {number}
*/
var totalFruit = function (fruits) {
const k = 2;
let res = 0;
// 记录窗口map 种类 => 出现次数
let map = new Map();
// 滑动窗口左右指针
let left = right = 0;
while (right < fruits.length) {
map.set(fruits[right], (map.get(fruits[right]) || 0) + 1)
while (map.size > k) {
map.set(fruits[left], (map.get(fruits[left]) - 1));
if (map.get(fruits[left]) === 0) {
map.delete(fruits[left]) // 🚩
}
left++
}
res = Math.max(res, right - left + 1);
right++
}
return res
};