-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 2516
- Loading branch information
Showing
3 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
leetcode/2501-2600/2516.Take-K-of-Each-Character-From-Left-and-Right/README.md
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,33 @@ | ||
# [2516.Take K of Each Character From Left and Right][title] | ||
|
||
## Description | ||
You are given a string `s` consisting of the characters `'a'`, `'b'`, and `'c'` and a non-negative integer `k`. Each minute, you may take either the **leftmost** character of `s`, or the **rightmost** character of `s`. | ||
|
||
Return the **minimum** number of minutes needed for you to take **at least** `k` of each character, or return `-1` if it is not possible to take `k` of each character. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: s = "aabaaaacaabc", k = 2 | ||
Output: 8 | ||
Explanation: | ||
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character. | ||
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters. | ||
A total of 3 + 5 = 8 minutes is needed. | ||
It can be proven that 8 is the minimum number of minutes needed. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: s = "a", k = 1 | ||
Output: -1 | ||
Explanation: It is not possible to take one 'b' or 'c' so return -1. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/take-k-of-each-character-from-left-and-right | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
63 changes: 61 additions & 2 deletions
63
leetcode/2501-2600/2516.Take-K-of-Each-Character-From-Left-and-Right/Solution.go
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 |
---|---|---|
@@ -1,5 +1,64 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "sort" | ||
|
||
func Solution(s string, k int) int { | ||
if k == 0 { | ||
return 0 | ||
} | ||
l := len(s) | ||
left, right := make([][3]int, l), make([][3]int, l) | ||
left[0][s[0]-'a'] = 1 | ||
for i := 1; i < l; i++ { | ||
left[i] = left[i-1] | ||
left[i][s[i]-'a']++ | ||
} | ||
right[l-1][s[l-1]-'a'] = 1 | ||
for i := l - 2; i >= 0; i-- { | ||
right[i] = right[i+1] | ||
right[i][s[i]-'a']++ | ||
} | ||
ans := -1 | ||
for i := l - 1; i > 0; i-- { | ||
a, b, c := right[i][0], right[i][1], right[i][2] | ||
if a >= k && b >= k && c >= k { | ||
if ans == -1 || ans > l-i { | ||
ans = l - i | ||
} | ||
continue | ||
} | ||
idx := sort.Search(i, func(ii int) bool { | ||
return left[ii][0]+a >= k && left[ii][1]+b >= k && left[ii][2]+c >= k | ||
}) | ||
if idx == i { | ||
continue | ||
} | ||
diff := l - i + idx + 1 | ||
if ans == -1 || ans > diff { | ||
ans = diff | ||
} | ||
} | ||
|
||
for i := 0; i < l-1; i++ { | ||
a, b, c := left[i][0], left[i][1], left[i][2] | ||
if a >= k && b >= k && c >= k { | ||
if ans == -1 || ans > i+1 { | ||
ans = i + 1 | ||
} | ||
continue | ||
} | ||
ll := l - i - 1 | ||
start := i + 1 | ||
idx := sort.Search(ll, func(ii int) bool { | ||
return right[start][0]+a >= k && right[start][1]+b >= k && right[start][2]+c >= k | ||
}) | ||
if idx == ll { | ||
continue | ||
} | ||
diff := i + 1 + l - i | ||
if ans == -1 || ans > diff { | ||
ans = diff | ||
} | ||
} | ||
return ans | ||
} |
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