- 문제설명
- 풀이코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include <string>
#include <vector>
//https://programmers.co.kr/learn/courses/30/lessons/12911
using namespace std;
string binary(int n){
if(n==1) return "1";
return binary(n/2) + to_string(n%2);
}
int get_one_cnt(int n){
string binary_n = binary(n);
int cnt = 0;
for(auto i : binary_n){
if(i=='1') cnt++;
}
return cnt;
}
int solution(int n) {
int answer = 0;
int n_one_cnt = get_one_cnt(n);
bool find_next_number = false;
int next_n = n+1;
while(find_next_number == false){
if(n_one_cnt == get_one_cnt(next_n)){
answer = next_n;
break;
}
next_n++;
}
return answer;
}
|
cs |
- 시간복잡도
O(n)
- 남의 풀이와 비교
이 문제의 경우는 문제그대로 2진수를 만들어서 실제로 1의 개수를 세는 방법말고는 딱히 방법이 없기때문에 검색을 통해서 비교해봤을때 거의 다 같은 방법으로 풀이가 되어있었다.
- Reference
'PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스 - 42898] 등굣길 (0) | 2020.11.09 |
---|---|
[프로그래머스 - 12913 ] 땅따먹기 (0) | 2020.11.08 |
[프로그래머스 - 68936] 쿼드압축 후 개수 세기 (0) | 2020.11.05 |
[프로그래머스 - 42861] 섬 연결하기 (0) | 2020.11.04 |
[프로그래머스 - 12952] N-Queen (0) | 2020.11.03 |