-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path904_fruit-into-baskets.js
74 lines (68 loc) · 2.43 KB
/
904_fruit-into-baskets.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
/**
*
* Problem:
* Given an array of characters where each character represents a fruit tree, you are given
* two baskets and your goal is to put maximum number of fruits in each basket. The only
* restriction is that each basket can have only one type of fruit.
* https://leetcode.com/problems/fruit-into-baskets/
*
* Example 1:
* Input: Fruit=['A', 'B', 'C', 'A', 'C']
* Output: 3
* Explanation: We can put 2 'C' in one basket and one 'A' in the other from the subarray
* ['C', 'A', 'C']
*
* Example 2:
* Input: Fruit=['A', 'B', 'C', 'B', 'B', 'C']
* Output: 5
* Explanation: We can put 3 'B' in one basket and two 'C' in the other basket. This can be
* done if we start with the second letter: ['B', 'C', 'B', 'B', 'C']
*
* Time: O(n) <- O(n + n)
* Space: O(n) <- map
*
* @param {string[]} fruitTrees
* @return {number}
*/
function fruitIntoBaskets(fruitTrees) {
const buckets = 2;
let windowStart = 0;
let windowEnd = 0;
let fruitTypeCountMapInWindow = {};
let maxFruitCount = 0;
// O(n) -> `windowEnd` run through all fruit types in the `fruitTrees`
while (windowEnd < fruitTrees.length) {
const endFruitType = fruitTrees[windowEnd];
// Aggregation is the count for each fruit type in the window
fruitTypeCountMapInWindow[endFruitType] =
(fruitTypeCountMapInWindow[endFruitType] || 0) + 1;
windowEnd++;
/**
* Keep shrinking the window if the fruit type in the window (key length of
* `fruitTypeCountMapInWindow`) is larger than k.
*
* Note:
* * O(n) in total -> `windowStart` run through all fruits in the `fruitTrees` for all
* the outer loops
*/
while (Object.keys(fruitTypeCountMapInWindow).length > buckets) {
const startFruitType = fruitTrees[windowStart];
fruitTypeCountMapInWindow[startFruitType]--;
if (fruitTypeCountMapInWindow[startFruitType] === 0) {
delete fruitTypeCountMapInWindow[startFruitType];
}
windowStart++;
}
/**
* Even the `fruitTypeCountMapInWindow` key length is less than `buckets`, we still need
* to return the result, because bucket might not be used
*
* Note: `windowEnd - windowStart` because `windowEnd` is not in window yet
*/
maxFruitCount = Math.max(maxFruitCount, windowEnd - windowStart);
}
return maxFruitCount;
}
// Test
console.log(fruitIntoBaskets(['A', 'B', 'C', 'A', 'C'])); // 3
console.log(fruitIntoBaskets(['A', 'B', 'C', 'B', 'B', 'C'])); // 5