Skip to content

Commit f6405c7

Browse files
committed
Add solution and test-cases for problem 3228
1 parent f03226b commit f6405c7

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [3228.Maximum Number of Operations to Move Ones to the End][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a binary string `s`.
5+
6+
You can perform the following operation on the string **any** number of times:
7+
8+
- Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`.
9+
- Move the character `s[i]` to the **right** until it reaches the end of the string or another `'1'`. For example, for `s = "010010"`, if we choose `i = 1`, the resulting string will be `s = "000110"`.
10+
11+
Return the **maximum** number of operations that you can perform.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: s = "1001101"
1417
15-
## 题意
16-
> ...
18+
Output: 4
1719
18-
## 题解
20+
Explanation:
1921
20-
### 思路1
21-
> ...
22-
Maximum Number of Operations to Move Ones to the End
23-
```go
22+
We can perform the following operations:
23+
24+
Choose index i = 0. The resulting string is s = "0011101".
25+
Choose index i = 4. The resulting string is s = "0011011".
26+
Choose index i = 3. The resulting string is s = "0010111".
27+
Choose index i = 2. The resulting string is s = "0001111".
2428
```
2529

30+
**Example 2:**
31+
32+
```
33+
Input: s = "00111"
34+
35+
Output: 0
36+
```
2637

2738
## 结语
2839

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
countOne := 0
5+
ans := 0
6+
i := 0
7+
for i < len(s) {
8+
if s[i] == '0' {
9+
for i+1 < len(s) && s[i+1] == '0' {
10+
i++
11+
}
12+
ans += countOne
13+
} else {
14+
countOne++
15+
}
16+
i++
17+
}
18+
return ans
519
}

leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "1001101", 4},
17+
{"TestCase2", "00111", 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)