Skip to content

Commit 03154b4

Browse files
committed
풀이: 릿코드.1888.Minimum Number of Flips to Make the Binary String Alternating
?: prefix sum을 이용해 풀이
1 parent d3110d5 commit 03154b4

3 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 1888. Minimum Number of Flips to Make the Binary String Alternating
2+
3+
[링크](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/description/)
4+
5+
| 난이도 |
6+
| :----: |
7+
| Medium |
8+
9+
## 설계
10+
11+
### 시간 복잡도
12+
13+
문자열의 길이를 N이라 하자.
14+
15+
모든 경우를 직접 계산할 경우 O(N^2)의 시간 복잡도를 사용하며 이는 제한시간 내에 불가능하다.
16+
17+
prefix suffix를 이용해 각 index별로 나누었을 때 변경횟수를 이용해 구할 경우 O(N)의 시간 복잡도를 사용한다.
18+
19+
### 공간 복잡도
20+
21+
prefix suffix 갯수만 저장할 경우 O(1)의 공간 복잡도를 사용한다.
22+
23+
### prefix suffix
24+
25+
| 내 코드 (ms) | 시간 복잡도 | 공간 복잡도 |
26+
| :----------: | :---------: | :---------: |
27+
| 12 | O(N) | O(1) |
28+
29+
특정 index를 기준으로 왼쪽과 오른쪽으로 나누어 생각할 수 있다.
30+
31+
왼쪽 부분을 이동시켜 오른쪽 부분 뒤에 붙인다고 가정하자.
32+
33+
이 경우 실제 시작 문자가 '0', '1'일 때 변경 횟수를 각각 구할 수 있다.
34+
35+
이후 변경된 문자열의 시작 문자에 따라 필요한 변경 횟수 중 작은 값을 반환한다.
36+
37+
문자열의 길이가 짝수인 경우는 왼쪽 부분을 이동시키더라도 시작 문자에 따라 규칙을 유지한다.
38+
39+
그러나 문자열의 길이가 홀수인 경우는 왼쪽 부분이 이동한 이후 시작 문자에 따른 규칙이 변경된다.
40+
41+
```cpp
42+
int minFlips(string s) {
43+
int size = s.size();
44+
45+
int fromLeft = 0;
46+
for (int i = 0; i < size; i++) {
47+
char target = (i % 2 + '0');
48+
if (target != s[i]) {
49+
fromLeft++;
50+
}
51+
}
52+
53+
int answer = min(fromLeft, size - fromLeft);
54+
55+
if (size % 2 == 1) {
56+
int fromRight = 0;
57+
for (int i = size - 1; i >= 0; i--) {
58+
char target = (i % 2 + '0');
59+
if (target != s[i]) {
60+
fromRight++;
61+
fromLeft--;
62+
}
63+
64+
answer = min(fromRight + (i - fromLeft), answer);
65+
answer = min(size - (fromRight + (i - fromLeft)), answer);
66+
}
67+
}
68+
69+
return answer;
70+
}
71+
```
72+
73+
## 고생한 점
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <algorithm>
2+
#include <climits>
3+
#include <cmath>
4+
#include <cstring>
5+
#include <functional>
6+
#include <iostream>
7+
#include <map>
8+
#include <numeric>
9+
#include <queue>
10+
#include <set>
11+
#include <stack>
12+
#include <string>
13+
#include <unordered_map>
14+
#include <unordered_set>
15+
#include <vector>
16+
17+
using namespace std;
18+
19+
// prefix suffix
20+
// time : O(N)
21+
// space : O(1)
22+
class Solution {
23+
public:
24+
int minFlips(string s) {
25+
int size = s.size();
26+
27+
int fromLeft = 0;
28+
for (int i = 0; i < size; i++) {
29+
char target = (i % 2 + '0');
30+
if (target != s[i]) {
31+
fromLeft++;
32+
}
33+
}
34+
35+
int answer = min(fromLeft, size - fromLeft);
36+
37+
if (size % 2 == 1) {
38+
int fromRight = 0;
39+
for (int i = size - 1; i >= 0; i--) {
40+
char target = (i % 2 + '0');
41+
if (target != s[i]) {
42+
fromRight++;
43+
fromLeft--;
44+
}
45+
46+
answer = min(fromRight + (i - fromLeft), answer);
47+
answer = min(size - (fromRight + (i - fromLeft)), answer);
48+
}
49+
}
50+
51+
return answer;
52+
}
53+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Input: s = "111000"
2+
Output: 2
3+
4+
===
5+
6+
Input: s = "010"
7+
Output: 0
8+
9+
===
10+
11+
Input: s = "1110"
12+
Output: 1
13+
14+
===
15+
16+
Input: "01001001101"
17+
Output: 2

0 commit comments

Comments
 (0)