Skip to content

Commit a3098ff

Browse files
authored
Merge pull request #1038 from 0xff-dev/2516
Add solution and test-cases for problem 2516
2 parents 8adbcbb + 0c04504 commit a3098ff

File tree

3 files changed

+104
-12
lines changed

3 files changed

+104
-12
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [2516.Take K of Each Character From Left and Right][title]
2+
3+
## Description
4+
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`.
5+
6+
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.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: s = "aabaaaacaabc", k = 2
12+
Output: 8
13+
Explanation:
14+
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
15+
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
16+
A total of 3 + 5 = 8 minutes is needed.
17+
It can be proven that 8 is the minimum number of minutes needed.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: s = "a", k = 1
24+
Output: -1
25+
Explanation: It is not possible to take one 'b' or 'c' so return -1.
26+
```
27+
28+
## 结语
29+
30+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
31+
32+
[title]: https://leetcode.com/problems/take-k-of-each-character-from-left-and-right
33+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(s string, k int) int {
6+
if k == 0 {
7+
return 0
8+
}
9+
l := len(s)
10+
left, right := make([][3]int, l), make([][3]int, l)
11+
left[0][s[0]-'a'] = 1
12+
for i := 1; i < l; i++ {
13+
left[i] = left[i-1]
14+
left[i][s[i]-'a']++
15+
}
16+
right[l-1][s[l-1]-'a'] = 1
17+
for i := l - 2; i >= 0; i-- {
18+
right[i] = right[i+1]
19+
right[i][s[i]-'a']++
20+
}
21+
ans := -1
22+
for i := l - 1; i > 0; i-- {
23+
a, b, c := right[i][0], right[i][1], right[i][2]
24+
if a >= k && b >= k && c >= k {
25+
if ans == -1 || ans > l-i {
26+
ans = l - i
27+
}
28+
continue
29+
}
30+
idx := sort.Search(i, func(ii int) bool {
31+
return left[ii][0]+a >= k && left[ii][1]+b >= k && left[ii][2]+c >= k
32+
})
33+
if idx == i {
34+
continue
35+
}
36+
diff := l - i + idx + 1
37+
if ans == -1 || ans > diff {
38+
ans = diff
39+
}
40+
}
41+
42+
for i := 0; i < l-1; i++ {
43+
a, b, c := left[i][0], left[i][1], left[i][2]
44+
if a >= k && b >= k && c >= k {
45+
if ans == -1 || ans > i+1 {
46+
ans = i + 1
47+
}
48+
continue
49+
}
50+
ll := l - i - 1
51+
start := i + 1
52+
idx := sort.Search(ll, func(ii int) bool {
53+
return right[start][0]+a >= k && right[start][1]+b >= k && right[start][2]+c >= k
54+
})
55+
if idx == ll {
56+
continue
57+
}
58+
diff := i + 1 + l - i
59+
if ans == -1 || ans > diff {
60+
ans = diff
61+
}
62+
}
63+
return ans
564
}

leetcode/2501-2600/2516.Take-K-of-Each-Character-From-Left-and-Right/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
s string
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "aabaaaacaabc", 2, 8},
18+
{"TestCase2", "a", 1, -1},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.s, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.s, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)