From 2da9729a8094e38b57e2c6139acaa33fad8ecd86 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 6 Jan 2025 09:14:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 1375 --- .../README.md | 42 +++++++++++++++++++ .../Solution.go | 13 +++++- .../Solution_test.go | 13 +++--- 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/README.md diff --git a/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/README.md b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/README.md new file mode 100644 index 000000000..6767317c1 --- /dev/null +++ b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/README.md @@ -0,0 +1,42 @@ +# [1375.Number of Times Binary String Is Prefix-Aligned][title] + +## Description +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. + +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. + +Return the number of times the binary string is **prefix-aligned** during the flipping process. + +**Example 1:** + +``` +Input: flips = [3,2,4,1,5] +Output: 2 +Explanation: The binary string is initially "00000". +After applying step 1: The string becomes "00100", which is not prefix-aligned. +After applying step 2: The string becomes "01100", which is not prefix-aligned. +After applying step 3: The string becomes "01110", which is not prefix-aligned. +After applying step 4: The string becomes "11110", which is prefix-aligned. +After applying step 5: The string becomes "11111", which is prefix-aligned. +We can see that the string was prefix-aligned 2 times, so we return 2. +``` + +**Example 2:** + +``` +Input: flips = [4,1,2,3] +Output: 1 +Explanation: The binary string is initially "0000". +After applying step 1: The string becomes "0001", which is not prefix-aligned. +After applying step 2: The string becomes "1001", which is not prefix-aligned. +After applying step 3: The string becomes "1101", which is not prefix-aligned. +After applying step 4: The string becomes "1111", which is prefix-aligned. +We can see that the string was prefix-aligned 1 time, so we return 1. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution.go b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution.go index d115ccf5e..c077ae8b0 100755 --- a/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution.go +++ b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution.go @@ -1,5 +1,14 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(flips []int) int { + ans := 0 + sum, cur := 0, 0 + for i, f := range flips { + sum += i + 1 + cur += f + if sum == cur { + ans++ + } + } + return ans } diff --git a/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution_test.go b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution_test.go index 14ff50eb4..72a16a822 100755 --- a/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution_test.go +++ b/leetcode/1301-1400/1375.Number-of-Times-Binary-String-Is-Prefix-Aligned/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 2, 4, 1, 5}, 2}, + {"TestCase2", []int{4, 1, 2, 3}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }