Skip to content

Commit 1d48b1c

Browse files
authored
feat: add solutions to lc problem: No.1529 (#4810)
1 parent a3afb25 commit 1d48b1c

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

solution/1500-1599/1529.Minimum Suffix Flips/README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ tags:
8181

8282
### 方法一:贪心
8383

84-
从前往后遍历 $target$,判断每个位置是否需要翻转,如果需要翻转,则翻转,并记录翻转次数
84+
我们从左到右遍历字符串 $\textit{target}$,用变量 $\textit{ans}$ 记录翻转次数。 当遍历到下标 $i$ 时,如果当前的翻转次数 $\textit{ans}$ 的奇偶性与 $\textit{target}[i]$ 不同,则需要在下标 $i$ 处进行一次翻转操作,将 $\textit{ans}$ 加 $1$
8585

86-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
86+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
8787

8888
<!-- tabs:start -->
8989

@@ -149,6 +149,37 @@ func minFlips(target string) int {
149149
}
150150
```
151151

152+
#### TypeScript
153+
154+
```ts
155+
function minFlips(target: string): number {
156+
let ans = 0;
157+
for (const c of target) {
158+
if (ans % 2 !== +c) {
159+
++ans;
160+
}
161+
}
162+
return ans;
163+
}
164+
```
165+
166+
#### Rust
167+
168+
```rust
169+
impl Solution {
170+
pub fn min_flips(target: String) -> i32 {
171+
let mut ans = 0;
172+
for c in target.chars() {
173+
let bit = (c as u8 - b'0') as i32;
174+
if ans % 2 != bit {
175+
ans += 1;
176+
}
177+
}
178+
ans
179+
}
180+
}
181+
```
182+
152183
<!-- tabs:end -->
153184

154185
<!-- solution:end -->

solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ We need at least 3 flip operations to form target.
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Greedy
77+
78+
We traverse the string $\textit{target}$ from left to right, using a variable $\textit{ans}$ to record the number of flips. When we reach index $i$, if the parity of the current flip count $\textit{ans}$ is different from $\textit{target}[i]$, we need to perform a flip operation at index $i$ and increment $\textit{ans}$ by $1$.
79+
80+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
7781

7882
<!-- tabs:start -->
7983

@@ -139,6 +143,37 @@ func minFlips(target string) int {
139143
}
140144
```
141145

146+
#### TypeScript
147+
148+
```ts
149+
function minFlips(target: string): number {
150+
let ans = 0;
151+
for (const c of target) {
152+
if (ans % 2 !== +c) {
153+
++ans;
154+
}
155+
}
156+
return ans;
157+
}
158+
```
159+
160+
#### Rust
161+
162+
```rust
163+
impl Solution {
164+
pub fn min_flips(target: String) -> i32 {
165+
let mut ans = 0;
166+
for c in target.chars() {
167+
let bit = (c as u8 - b'0') as i32;
168+
if ans % 2 != bit {
169+
ans += 1;
170+
}
171+
}
172+
ans
173+
}
174+
}
175+
```
176+
142177
<!-- tabs:end -->
143178

144179
<!-- solution:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn min_flips(target: String) -> i32 {
3+
let mut ans = 0;
4+
for c in target.chars() {
5+
let bit = (c as u8 - b'0') as i32;
6+
if ans % 2 != bit {
7+
ans += 1;
8+
}
9+
}
10+
ans
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function minFlips(target: string): number {
2+
let ans = 0;
3+
for (const c of target) {
4+
if (ans % 2 !== +c) {
5+
++ans;
6+
}
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)