Skip to content

Commit 183d2c1

Browse files
authored
Merge pull request #1085 from 0xff-dev/1375
Add solution and test-cases for problem 1375
2 parents 2bea406 + 2da9729 commit 183d2c1

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [1375.Number of Times Binary String Is Prefix-Aligned][title]
2+
3+
## Description
4+
You have a **1-indexed** binary string of length `n` where all the bits are `0` initially. We will flip all the bits of this binary string (i.e., change them from `0` to `1`) one by one. You are given a **1-indexed** integer array `flips` where `flips[i]` indicates that the bit at index `i` will be flipped in the `ith` step.
5+
6+
A binary string is **prefix-aligned** if, after the `ith` step, all the bits in the **inclusive** range `[1, i]` are ones and all the other bits are zeros.
7+
8+
Return the number of times the binary string is **prefix-aligned** during the flipping process.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: flips = [3,2,4,1,5]
14+
Output: 2
15+
Explanation: The binary string is initially "00000".
16+
After applying step 1: The string becomes "00100", which is not prefix-aligned.
17+
After applying step 2: The string becomes "01100", which is not prefix-aligned.
18+
After applying step 3: The string becomes "01110", which is not prefix-aligned.
19+
After applying step 4: The string becomes "11110", which is prefix-aligned.
20+
After applying step 5: The string becomes "11111", which is prefix-aligned.
21+
We can see that the string was prefix-aligned 2 times, so we return 2.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: flips = [4,1,2,3]
28+
Output: 1
29+
Explanation: The binary string is initially "0000".
30+
After applying step 1: The string becomes "0001", which is not prefix-aligned.
31+
After applying step 2: The string becomes "1001", which is not prefix-aligned.
32+
After applying step 3: The string becomes "1101", which is not prefix-aligned.
33+
After applying step 4: The string becomes "1111", which is prefix-aligned.
34+
We can see that the string was prefix-aligned 1 time, so we return 1.
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(flips []int) int {
4+
ans := 0
5+
sum, cur := 0, 0
6+
for i, f := range flips {
7+
sum += i + 1
8+
cur += f
9+
if sum == cur {
10+
ans++
11+
}
12+
}
13+
return ans
514
}

leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/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 []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 2, 4, 1, 5}, 2},
17+
{"TestCase2", []int{4, 1, 2, 3}, 1},
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)