Skip to content

Commit 530c260

Browse files
committed
Add solution and test-cases for problem 3234
1 parent f03226b commit 530c260

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
# [3234.Count the Number of Substrings With Dominant Ones][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+
Return the number of substrings with **dominant** ones.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
Input: s = "00011"
1414
15-
## 题意
16-
> ...
15+
Output: 5
1716
18-
## 题解
17+
Explanation:
1918
20-
### 思路1
21-
> ...
22-
Count the Number of Substrings With Dominant Ones
23-
```go
19+
The substrings with dominant ones are shown in the table below.
20+
21+
i j s[i..j] Number of Zeros Number of Ones
22+
3 3 1 0 1
23+
4 4 1 0 1
24+
2 3 01 1 1
25+
3 4 11 0 2
26+
2 4 011 1 2
2427
```
2528

29+
**Example 2:**
30+
31+
```
32+
Input: s = "101101"
33+
34+
Output: 16
35+
36+
Explanation:
37+
38+
The substrings with non-dominant ones are shown in the table below.
39+
40+
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
41+
42+
i j s[i..j] Number of Zeros Number of Ones
43+
1 1 0 1 0
44+
4 4 0 1 0
45+
1 4 0110 2 2
46+
0 4 10110 2 3
47+
1 5 01101 2 3
48+
```
2649

2750
## 结语
2851

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
n := len(s)
5+
pre := make([]int, n+1)
6+
pre[0] = -1
7+
for i := 0; i < n; i++ {
8+
if i == 0 || (i > 0 && s[i-1] == '0') {
9+
pre[i+1] = i
10+
} else {
11+
pre[i+1] = pre[i]
12+
}
13+
}
14+
res := 0
15+
for i := 1; i <= n; i++ {
16+
cnt0 := 0
17+
if s[i-1] == '0' {
18+
cnt0 = 1
19+
}
20+
j := i
21+
for j > 0 && cnt0*cnt0 <= n {
22+
cnt1 := (i - pre[j]) - cnt0
23+
if cnt0*cnt0 <= cnt1 {
24+
add := j - pre[j]
25+
if cnt1-cnt0*cnt0+1 < add {
26+
add = cnt1 - cnt0*cnt0 + 1
27+
}
28+
res += add
29+
}
30+
j = pre[j]
31+
cnt0++
32+
}
33+
}
34+
return res
535
}

leetcode/3201-3300/3234.Count-the-Number-of-Substrings-With-Dominant-Ones/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", "00011", 5},
17+
{"TestCase2", "101101", 16},
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)