일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Queue
- 코딩테스트
- 코테
- 시간복잡도
- coding test
- 코딩
- itertools
- 파이썬
- 프로그래머스
- BFS
- 연습문제
- lv4
- import re
- 조합
- sql
- 데이터분석
- collections
- lambda
- Stack
- programmers
- coding
- time complexity
- Level2
- mysql
- python
- 완전탐색
- cte
- level4
- counter
- join
- Today
- Total
ror_coding
[Programmers] 점프와 순간 이동 - 12980 본문
728x90
2로 나눠갈 때 홀수인 경우를 bin().count('1')로 구한다.. 천잰가..?
Question
아이언 슈트 구매자는 아이언 슈트를 착용하고 거리가 N 만큼 떨어져 있는 장소로 가려고 합니다. 단, 건전지 사용량을 줄이기 위해 점프로 이동하는 것은 최소로 하려고 합니다. 아이언 슈트 구매자가 이동하려는 거리 N이 주어졌을 때, 사용해야 하는 건전지 사용량의 최솟값을 return하는 solution 함수를 만들어 주세요.
Code (After)
def solution(n):
answer = 0
while n > 0:
answer += n%2
n = n//2
return answer
Code 1 : bin() 으로 홀수일 때 count.
def solution(n):
return bin(n).count('1')
Code 2 : my code (Before)
def solution(n):
answer = 0
while n>0 :
if n % 2 == 0: n = n//2; continue
else : n -= 1; answer += 1
return answer

now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 예상 대진표 - 12985 (0) | 2024.10.31 |
---|---|
[Programmers] 멀리 뛰기 - 12914 (1) | 2024.10.31 |
[Programmers] 올바른 괄호 - 12909, 짝지어 제거하기 - 12973 (0) | 2024.10.28 |
[Programmers] 신고 결과 받기 - 92334 (0) | 2024.10.28 |
[Programmers] 개인정보 수집 유효기간 - 150370 (1) | 2024.10.26 |