Skip to content

Commit 1c3980f

Browse files
authored
feat: add solutions for lc No.1758 (#5062)
1 parent f390b32 commit 1c3980f

5 files changed

Lines changed: 77 additions & 56 deletions

File tree

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ tags:
6464

6565
### 方法一:一次遍历
6666

67-
根据题意,如果得到交替字符串 `01010101...` 所需要的操作数为 $cnt$,那么得到交替字符串 `10101010...` 所需要的操作数为 $n - cnt$。
67+
根据题意,如果得到交替字符串 `01010101...` 所需要的操作数为 $\textit{cnt}$,那么得到交替字符串 `10101010...` 所需要的操作数为 $n - \textit{cnt}$。
6868

69-
因此,我们只需要遍历一次字符串 $s$,统计出 $cnt$ 的值,那么答案即为 $\min(cnt, n - cnt)$。
69+
因此,我们只需要遍历一次字符串 $s$,统计出 $\textit{cnt}$ 的值,那么答案即为 $\min(\textit{cnt}, n - \textit{cnt})$。
7070

71-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
71+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
7272

7373
<!-- tabs:start -->
7474

@@ -126,12 +126,14 @@ func minOperations(s string) int {
126126

127127
```ts
128128
function minOperations(s: string): number {
129+
let cnt = 0;
129130
const n = s.length;
130-
let count = 0;
131-
for (let i = 0; i < n; i++) {
132-
count += s[i] !== '01'[i & 1] ? 1 : 0;
131+
for (let i = 0; i < n; ++i) {
132+
if (s[i] !== "01"[i & 1]) {
133+
++cnt;
134+
}
133135
}
134-
return Math.min(count, n - count);
136+
return Math.min(cnt, n - cnt);
135137
}
136138
```
137139

@@ -140,30 +142,33 @@ function minOperations(s: string): number {
140142
```rust
141143
impl Solution {
142144
pub fn min_operations(s: String) -> i32 {
143-
let n = s.len();
144-
let s = s.as_bytes();
145-
let cs = [b'0', b'1'];
146-
let mut count = 0;
147-
for i in 0..n {
148-
count += if s[i] != cs[i & 1] { 1 } else { 0 };
145+
let mut cnt: i32 = 0;
146+
let n: i32 = s.len() as i32;
147+
let bytes = s.as_bytes();
148+
149+
for i in 0..n as usize {
150+
if bytes[i] != b"01"[i & 1] {
151+
cnt += 1;
152+
}
149153
}
150-
count.min(n - count) as i32
154+
155+
cnt.min(n - cnt)
151156
}
152157
}
153158
```
154159

155160
#### C
156161

157162
```c
158-
#define min(a, b) (((a) < (b)) ? (a) : (b))
159-
160163
int minOperations(char* s) {
164+
int cnt = 0;
161165
int n = strlen(s);
162-
int count = 0;
163-
for (int i = 0; i < n; i++) {
164-
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
166+
for (int i = 0; i < n; ++i) {
167+
if (s[i] != "01"[i & 1]) {
168+
++cnt;
169+
}
165170
}
166-
return min(count, n - count);
171+
return cnt < (n - cnt) ? cnt : (n - cnt);
167172
}
168173
```
169174

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README_EN.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Single Pass
67+
68+
According to the problem, if the number of operations needed to obtain the alternating string `01010101...` is $\textit{cnt}$, then the number of operations needed to obtain the alternating string `10101010...` is $n - \textit{cnt}$.
69+
70+
Therefore, we only need to traverse the string $s$ once, count the value of $\textit{cnt}$, and the answer is $\min(\textit{cnt}, n - \textit{cnt})$.
71+
72+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
6773

6874
<!-- tabs:start -->
6975

@@ -121,12 +127,14 @@ func minOperations(s string) int {
121127

122128
```ts
123129
function minOperations(s: string): number {
130+
let cnt = 0;
124131
const n = s.length;
125-
let count = 0;
126-
for (let i = 0; i < n; i++) {
127-
count += s[i] !== '01'[i & 1] ? 1 : 0;
132+
for (let i = 0; i < n; ++i) {
133+
if (s[i] !== "01"[i & 1]) {
134+
++cnt;
135+
}
128136
}
129-
return Math.min(count, n - count);
137+
return Math.min(cnt, n - cnt);
130138
}
131139
```
132140

@@ -135,30 +143,33 @@ function minOperations(s: string): number {
135143
```rust
136144
impl Solution {
137145
pub fn min_operations(s: String) -> i32 {
138-
let n = s.len();
139-
let s = s.as_bytes();
140-
let cs = [b'0', b'1'];
141-
let mut count = 0;
142-
for i in 0..n {
143-
count += if s[i] != cs[i & 1] { 1 } else { 0 };
146+
let mut cnt: i32 = 0;
147+
let n: i32 = s.len() as i32;
148+
let bytes = s.as_bytes();
149+
150+
for i in 0..n as usize {
151+
if bytes[i] != b"01"[i & 1] {
152+
cnt += 1;
153+
}
144154
}
145-
count.min(n - count) as i32
155+
156+
cnt.min(n - cnt)
146157
}
147158
}
148159
```
149160

150161
#### C
151162

152163
```c
153-
#define min(a, b) (((a) < (b)) ? (a) : (b))
154-
155164
int minOperations(char* s) {
165+
int cnt = 0;
156166
int n = strlen(s);
157-
int count = 0;
158-
for (int i = 0; i < n; i++) {
159-
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
167+
for (int i = 0; i < n; ++i) {
168+
if (s[i] != "01"[i & 1]) {
169+
++cnt;
170+
}
160171
}
161-
return min(count, n - count);
172+
return cnt < (n - cnt) ? cnt : (n - cnt);
162173
}
163174
```
164175
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#define min(a, b) (((a) < (b)) ? (a) : (b))
2-
31
int minOperations(char* s) {
2+
int cnt = 0;
43
int n = strlen(s);
5-
int count = 0;
6-
for (int i = 0; i < n; i++) {
7-
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
4+
for (int i = 0; i < n; ++i) {
5+
if (s[i] != "01"[i & 1]) {
6+
++cnt;
7+
}
88
}
9-
return min(count, n - count);
10-
}
9+
return cnt < (n - cnt) ? cnt : (n - cnt);
10+
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
impl Solution {
22
pub fn min_operations(s: String) -> i32 {
3-
let n = s.len();
4-
let s = s.as_bytes();
5-
let cs = [b'0', b'1'];
6-
let mut count = 0;
7-
for i in 0..n {
8-
count += if s[i] != cs[i & 1] { 1 } else { 0 };
3+
let mut cnt: i32 = 0;
4+
let n: i32 = s.len() as i32;
5+
let bytes = s.as_bytes();
6+
7+
for i in 0..n as usize {
8+
if bytes[i] != b"01"[i & 1] {
9+
cnt += 1;
10+
}
911
}
10-
count.min(n - count) as i32
12+
13+
cnt.min(n - cnt)
1114
}
1215
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function minOperations(s: string): number {
2+
let cnt = 0;
23
const n = s.length;
3-
let count = 0;
4-
for (let i = 0; i < n; i++) {
5-
count += s[i] !== '01'[i & 1] ? 1 : 0;
4+
for (let i = 0; i < n; ++i) {
5+
if (s[i] !== "01"[i & 1]) {
6+
++cnt;
7+
}
68
}
7-
return Math.min(count, n - count);
9+
return Math.min(cnt, n - cnt);
810
}

0 commit comments

Comments
 (0)