
단순히 이전 값과 비교하여 다르면 +1을 하는 구조로 풀 수 있다.
마지막에는 1을 뒤집는데 드는 비용, 0을 뒤집는데 드는 비용 중 최솟값을 출력하면 된다.
#include <iostream>
#define endl "\n"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int zero_cnt = 0, one_cnt = 0;
char before = '\0';
for (char c : s) {
if (before != c) {
if (c == '0') { zero_cnt++; }
else if (c == '1') { one_cnt++; }
before = c;
}
}
cout << min(zero_cnt, one_cnt) << endl;
}'Algorithm > BOJ' 카테고리의 다른 글
| [백준] 2847번 Silver 5 게임을 만든 동준이, C++ 코드 (0) | 2025.12.16 |
|---|---|
| [백준] 1005번 Gold 3 ACM Craft, C++ 코드 (0) | 2025.12.14 |
| [백준] 2252번 Gold 3 줄 세우기, C++ 코드 (0) | 2025.12.14 |
| [백준] 2623번 Gold 3 음악프로그램, C++ (0) | 2025.12.14 |
| [백준] 2524번 Gold 5 괄호의 값, C++ 코드 (0) | 2025.12.12 |