-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hoanh An
committed
Dec 15, 2019
1 parent
9978973
commit 42d01ba
Showing
2 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 leetcode | ||
|
||
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 | ||
} |