문제접근
•
연속된 숫자열을 뒤집는 것이기 때문에 연속된 각각 0과 1로만 이루어진 부분들이 몇개가 있는지를 파악한다
•
0으로만 된 그룹, 1로만 된 그룹의 개수를 구해서 더 많은 그룹을 뒤집으면 더 적은 횟수로 같은 숫자열을 만들 수 있다
놓쳤던 부분
코드
2024 KB
0 ms
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void) {
string input;
int countOne = 0;
int countTwo = 0;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> input;
unsigned int inputIndex = 0;
char current = input[0];
while (++inputIndex < input.size()) {
if (input[inputIndex] != current) {
if (current == '1')
countOne++;
else
countTwo++;
current = input[inputIndex];
}
}
cout << max(countOne, countTwo);
return (0);
}
C++
복사