diff --git a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/README.md b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/README.md index d17dd1046..98ea9dbf7 100755 --- a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/README.md +++ b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/README.md @@ -1,28 +1,51 @@ # [3234.Count the Number of Substrings With Dominant Ones][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`. + +Return the number of substrings with **dominant** ones. + +A string has **dominant** ones if the number of ones in the string is **greater than or equal to** the **square** of the number of zeros in the string. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: s = "00011" -## 题意 -> ... +Output: 5 -## 题解 +Explanation: -### 思路1 -> ... -Count the Number of Substrings With Dominant Ones -```go +The substrings with dominant ones are shown in the table below. + +i j s[i..j] Number of Zeros Number of Ones +3 3 1 0 1 +4 4 1 0 1 +2 3 01 1 1 +3 4 11 0 2 +2 4 011 1 2 ``` +**Example 2:** + +``` +Input: s = "101101" + +Output: 16 + +Explanation: + +The substrings with non-dominant ones are shown in the table below. + +Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones. + +i j s[i..j] Number of Zeros Number of Ones +1 1 0 1 0 +4 4 0 1 0 +1 4 0110 2 2 +0 4 10110 2 3 +1 5 01101 2 3 +``` ## 结语 diff --git a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution.go b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution.go index d115ccf5e..345d9e25b 100644 --- a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution.go +++ b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + n := len(s) + pre := make([]int, n+1) + pre[0] = -1 + for i := 0; i < n; i++ { + if i == 0 || (i > 0 && s[i-1] == '0') { + pre[i+1] = i + } else { + pre[i+1] = pre[i] + } + } + res := 0 + for i := 1; i <= n; i++ { + cnt0 := 0 + if s[i-1] == '0' { + cnt0 = 1 + } + j := i + for j > 0 && cnt0*cnt0 <= n { + cnt1 := (i - pre[j]) - cnt0 + if cnt0*cnt0 <= cnt1 { + add := j - pre[j] + if cnt1-cnt0*cnt0+1 < add { + add = cnt1 - cnt0*cnt0 + 1 + } + res += add + } + j = pre[j] + cnt0++ + } + } + return res } diff --git a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution_test.go b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution_test.go index 14ff50eb4..1108dbd33 100644 --- a/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/Solution_test.go +++ b/leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/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", "00011", 5}, + {"TestCase2", "101101", 16}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }