0과 1로 이루어진 문자열 s가 "1"이 될 때까지 다음 과정을 반복하며 [이진 변환 횟수, 제거된 0의 총합]을 구하는 문제입니다.
c를 2진수 문자열로 변환하여 s를 교체합니다.0을 만날 때마다 길이를 줄이고, to_string을 이용해 이진수 문자열을 새로 생성합니다.#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(string s) {
int count = 0;
int zero_count = 0;
while(s.length() != 1) {
int temp = s.length();
for(int i = 0; i < s.length(); i++) {
if(s[i] == '0') {
temp -= 1;
zero_count += 1;
}
}
string binary = "";
while (temp > 0) {
binary += to_string(temp % 2);
temp /= 2;
}
reverse(binary.begin(), binary.end());
s = binary;
count += 1;
}
return {count, zero_count};
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(string s) {
int transform = 0;
int removedZero = 0;
while (s != "1") {
int ones = 0;
for (char ch : s) {
if (ch == '1') ones++;
else removedZero++;
}
s.clear();
while (ones > 0) {
s.push_back(char('0' + (ones % 2)));
ones /= 2;
}
reverse(s.begin(), s.end());
transform++;
}
return {transform, removedZero};
}
#include <string>
#include <vector>
using namespace std;
vector<int> solution(string s) {
int transform = 0;
int removedZero = 0;
int n = (int)s.size();
int ones = 0;
for (char ch : s) if (ch == '1') ones++;
while (n > 1) {
removedZero += (n - ones);
transform++;
int nextN = 0;
int nextOnes = 0;
int x = ones;
while (x > 0) {
nextN++;
if (x & 1) nextOnes++;
x >>= 1;
}
n = nextN;
ones = nextOnes;
}
return {transform, removedZero};
}
| 구분 | 원본 코드 (Initial) | 정석 코드 1 (Standard) | 정석 코드 2 (Optimized) |
|---|---|---|---|
| 0 제거 방식 | 반복문을 돌며 temp--로 남은 길이 계산 | ones++로 1의 개수를 직접 카운트 | n - ones 수식을 통해 제거된 개수 즉시 산출 |
| 문자열 생성 | 매 루프마다 to_string으로 새 문자열 생성 | push_back과 reverse를 활용해 기존 객체 재사용 | 문자열 생성을 완전히 배제하고 정수(int)로만 연산 |
| 메모리 효율 | 문자열 재할당으로 인해 메모리 사용량 높음 | 메모리 할당을 최소화하여 안정적임 | 최적. 스택 메모리 내 정수 연산만 수행 |
| 핵심 장점 | 로직이 직관적이라 구현이 빠름 | 가독성과 성능의 균형이 좋아 코테 정답의 정석 | 복사 비용을 극한으로 줄인 설계 (High Performance) |
| 추천 상황 | 알고리즘 초안 작성 시 | 실전 코딩 테스트 제출용 | 대용량 데이터 처리 및 성능 최적화 어필 시 |
문제를 풀 때 "실제 데이터(문자열)를 변형해야 하는가?" 아니면 "데이터의 속성(길이, 개수)만 필요한가?"를 고민해보는 것만으로도 훨씬 효율적인 코드를 작성할 수 있습니다.
이번 문제는 후자에 해당하여, 문자열을 생성하지 않는 정석 코드 2 방식이 가장 우수한 성능을 보여줍니다.