Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 포스코 아카데미
- 추천시스템
- 직원 이직률
- 삼성SDS
- 포스코 청년
- 영상제작기
- 데이터 분석
- 데이터분석
- 삼성SDS Brigthics
- 모델링
- 혼공머신러닝딥러닝
- Brigthics를 이용한 분석
- 혼공머신
- Brightics Studio
- Brightics
- 팀 분석
- 삼성 SDS Brigthics
- 개인 의료비 예측
- 캐글
- 브라이틱스
- 브라이틱스 서포터즈
- 삼성SDS Brightics
- Brigthics
- 삼성 SDS
- 직원 이직여부
- Brightics를 이용한 분석
- 혼공학습단
- 혼공
- Brigthics Studio
- 노코드AI
Archives
- Today
- Total
데이터사이언스 기록기📚
[프로그래머스/Python] Level 2_다음 큰 숫자(연습문제) 본문
📌문제 유형
연습문제
📌문제
📌나의 문제풀이
def solution(n):
answer = 0
num_list = []
# 이진수 1,0 개수 세기
n_to_b = format(n,'b')
n_cnt_1 = n_to_b.count('1')
n_cnt_0 = n_to_b.count('0')
# 0의 개수가 있는 경우
if n_cnt_0:
# n을 하나씩 더해가면서 이진수로 바꾸기
while n < 1000001:
n += 1
new_b = format(n,'b')
new_cnt_1 = new_b.count('1')
# new_cnt_1와 n_cnt_1 개수 같은 경우 리턴
if new_cnt_1 == n_cnt_1:
return n
# 0의 개수가 없는 경우
else:
num = ['1' for _ in range(n_cnt_1)]
# 1사이에 0 넣어보기
for i in range(1,n_cnt_1+1):
num.insert(i, '0')
num_b = ''.join(num)
num_list.append(int(num_b,2))
# 가장 작은 수 출력
return min(num_list)
📌다른 사람의 문제풀이
1) n을 하나씩 더한 후 1의 개수 비교
def nextBigNumber(n):
num1 = bin(n).count('1')
while True:
n = n + 1
if num1 == bin(n).count('1'):
break
return n
2) 1)과 유사 풀이
def nextBigNumber(n):
c = bin(n).count('1')
for m in range(n+1,1000001):
if bin(m).count('1') == c:
return m
📌리뷰
- 반드시 이진수 -> 10진수가 아닌, 역으로도 생각해보기
- 이진수로 변환 : n_to_b = format(n,'b')
- 이진수 -> 10진수로 변환 : int(num_b,2)
- 이진수도 count() 함수 사용 가능
728x90
'Coding Test > 프로그래머스(Python)' 카테고리의 다른 글
[프로그래머스/Python] Level 2_[3차] 파일명 정렬(2018 KAKAO BLIND RECRUITMENT) (0) | 2023.05.17 |
---|---|
[프로그래머스/Python] Level 2_게임 맵 최단거리(DFS/BFS) (0) | 2023.05.16 |
[프로그래머스/Python] Level 2_스킬트리(Summer/Winter Coding(~2018)) (0) | 2022.11.04 |
[프로그래머스/Python] Level 2_오픈채팅방(2019 KAKAO BLIND RECRUITMENT) (0) | 2022.10.30 |
[프로그래머스/Python] Level 2_주차 요금 계산(2022 KAKAO BLIND RECRUITMENT) (0) | 2022.10.30 |
Comments