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 | 29 | 30 | 31 |
Tags
- Stack
- SQL 고득점 KIT
- DP
- 다이나믹프로그래밍
- 소프티어
- bfs
- Python
- Lv. 1
- 너비 우선 탐색
- softeer
- 타입스크립트
- Java
- Lv. 3
- javascript
- level3
- programmers
- 브루트포스
- group by
- 문자열
- 프로그래머스
- C언어
- Typescript
- level 3
- Lv. 0
- Dynamic Programming
- dfs
- 스택
- Lv. 2
- 파이썬
- 자바스크립트
Archives
- Today
- Total
몸과 마음이 건전한 SW 개발자
프로그래머스 [Lv. 2] 유사 칸토어 비트열 {언어 : Python} 본문
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/148652
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답 코드
def solution(n, l, r):
def calSquare(number):
square = 0
while number:
square += 1
number //= 5
return square
squareL = calSquare(l-1)
squareR = calSquare(r)
def countOne(number, square):
cnt = 0
while number:
tmp = 5 ** square
share = number // tmp
remainder = number % tmp
if share > 2:
cnt += (share - 1) * 4 ** square
elif share < 2:
cnt += share * 4 ** square
else:
cnt += share * 4 ** square
remainder = 0
if remainder < 6:
if remainder > 2:
cnt += remainder - 1
else:
cnt += remainder
break
number = remainder
square -= 1
return cnt
cntL = countOne(l-1, squareL)
cntR = countOne(r, squareR)
answer = cntR - cntL
return answer
풀이 방법
n | 유사 칸토어 | 규칙 | 1의 개수 |
0 | 1 | 1 | 1 |
1 | 11011 | 1 1 0 1 1 | 4 |
2 | 1101111011000001101111011 | 4 4 0 4 4 | 16 |
3 | 1101111011000001101111011 110111101100000110111101100000000000000000000000 1101111011000001101111011 1101111011000001101111011 | 16 16 0 16 16 | 64 |
- 위와 같이 전체 1의 개수를 유추할 수 있다.
- 계산식이 복잡하여 이미지로 보도록 하자
- 몫이 2일 때 나머지를 버리는 이유는 몫이 2일 때 0만 있는 위치에 해당하기 때문이다.
- 몫이 3일 때는 0만 있는 위치를 빼기 때문에 몫에서 1을 빼준다.
느낀점
- 규칙을 알면 그나마 쉽게 풀리는 문제
'알고리즘' 카테고리의 다른 글
프로그래머스 [Lv. 2] 디펜스 게임 {언어 : Python} [반례 포함] (1) | 2024.04.21 |
---|---|
프로그래머스 [Lv. 2] 테이블 해시 함수 {언어 : JavaScript} (0) | 2024.04.20 |
프로그래머스 [Lv. 2] 숫자 변환하기 {언어 : JavaScript} [성능 개선 필요] (0) | 2024.04.17 |
프로그래머스 [Lv. 2] 무인도 여행 {언어 : JavaScript} (0) | 2024.04.15 |
프로그래머스 [Lv. 2] 호텔 대실 {언어 : Python} [heap X] (0) | 2024.04.14 |