Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions solution/1500-1599/1529.Minimum Suffix Flips/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ tags:

### 方法一:贪心

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -149,6 +149,37 @@ func minFlips(target string) int {
}
```

#### TypeScript

```ts
function minFlips(target: string): number {
let ans = 0;
for (const c of target) {
if (ans % 2 !== +c) {
++ans;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn min_flips(target: String) -> i32 {
let mut ans = 0;
for c in target.chars() {
let bit = (c as u8 - b'0') as i32;
if ans % 2 != bit {
ans += 1;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
37 changes: 36 additions & 1 deletion solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ We need at least 3 flip operations to form target.

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

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$.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -139,6 +143,37 @@ func minFlips(target string) int {
}
```

#### TypeScript

```ts
function minFlips(target: string): number {
let ans = 0;
for (const c of target) {
if (ans % 2 !== +c) {
++ans;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn min_flips(target: String) -> i32 {
let mut ans = 0;
for c in target.chars() {
let bit = (c as u8 - b'0') as i32;
if ans % 2 != bit {
ans += 1;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
12 changes: 12 additions & 0 deletions solution/1500-1599/1529.Minimum Suffix Flips/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn min_flips(target: String) -> i32 {
let mut ans = 0;
for c in target.chars() {
let bit = (c as u8 - b'0') as i32;
if ans % 2 != bit {
ans += 1;
}
}
ans
}
}
9 changes: 9 additions & 0 deletions solution/1500-1599/1529.Minimum Suffix Flips/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function minFlips(target: string): number {
let ans = 0;
for (const c of target) {
if (ans % 2 !== +c) {
++ans;
}
}
return ans;
}