-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathfruits_baskets_test.go
86 lines (69 loc) · 2.12 KB
/
fruits_baskets_test.go
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
/*
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.
Constraints:
- Each basket can have only one type of fruit.
- You can start with any tree, but once you have started you can’t skip a tree.
You will pick one fruit from each tree until you cannot, i.e., you will stop
when you have to pick from a third fruit type.
Example:
- Input: fruits=["apple", "orange", "coconut", "apple", "coconut"]
Output: 3
Explanation: Can put 2 "cocunut" in 1 basket and 1 "apple" in other from
subarray ["coconut", "apple", "coconut"]
Approach:
- Similar to "longest substring with k distinct characters" with k=2.
Cost:
- O(n) time, O(k) space where k is the number of characters in the map.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestFruitsIntoBaskets(t *testing.T) {
tests := []struct {
in []string
expected int
}{
{[]string{"apple", "orange", "coconut", "apple", "coconut"}, 3},
{[]string{"apple", "orange", "coconut", "orange", "coconut", "orange", "coconut"}, 6},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
fruitsIntoBaskets(tt.in),
)
}
}
func fruitsIntoBaskets(fruits []string) int {
maxLength, start := 0, 0
// fruitsMap keeps track of fruits' frequencies.
fruitsMap := map[string]int{}
for end := range fruits {
// insert fruits until we have 2 distinct fruits.
endFruit := fruits[end]
if _, ok := fruitsMap[endFruit]; !ok {
fruitsMap[endFruit] = 0
}
fruitsMap[endFruit]++
// shrink the window until there is no more than 2 distinct fruits.
for len(fruitsMap) > 2 {
startFruit := fruits[start]
// decrement the frequency of the one going out of the window and
// remove if its frequency is zero.
fruitsMap[startFruit]--
if fruitsMap[startFruit] == 0 {
delete(fruitsMap, startFruit)
}
// increase the start index to move the window ahead by one element.
start++
}
// update the maximum length at each step.
maxLength = common.Max(maxLength, end-start+1)
}
return maxLength
}