Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2516 #1038

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
s string
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "aabaaaacaabc", 2, 8},
{"TestCase2", "a", 1, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.s, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.s, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading