diff --git a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/README.md b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/README.md index 5b00aedbc..f9218b48e 100755 --- a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/README.md +++ b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/README.md @@ -1,28 +1,39 @@ # [3228.Maximum Number of Operations to Move Ones to the End][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a binary string `s`. + +You can perform the following operation on the string **any** number of times: + +- Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`. +- 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"`. + +Return the **maximum** number of operations that you can perform. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: s = "1001101" -## 题意 -> ... +Output: 4 -## 题解 +Explanation: -### 思路1 -> ... -Maximum Number of Operations to Move Ones to the End -```go +We can perform the following operations: + +Choose index i = 0. The resulting string is s = "0011101". +Choose index i = 4. The resulting string is s = "0011011". +Choose index i = 3. The resulting string is s = "0010111". +Choose index i = 2. The resulting string is s = "0001111". ``` +**Example 2:** + +``` +Input: s = "00111" + +Output: 0 +``` ## 结语 diff --git a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution.go b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution.go index d115ccf5e..7ad53eb5b 100644 --- a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution.go +++ b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + countOne := 0 + ans := 0 + i := 0 + for i < len(s) { + if s[i] == '0' { + for i+1 < len(s) && s[i+1] == '0' { + i++ + } + ans += countOne + } else { + countOne++ + } + i++ + } + return ans } diff --git a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution_test.go b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution_test.go index 14ff50eb4..7866a59d2 100644 --- a/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution_test.go +++ b/leetcode/3201-3300/3228.Maximum-Number-of-Operations-to-Move-Ones-to-the-End/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "1001101", 4}, + {"TestCase2", "00111", 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }