일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- heapq
- 다익스트라
- 동적 계획법
- divmod
- 백준
- Combinations
- 재귀함수
- 파이썬
- BFS
- backjoon
- KAKAO BLIND RECRUITMENT
- Re
- 그리디
- 수학
- dfs
- programmers
- python
- 카카오
- Set
- DateTime
- 정규식
- 이분탐색
- 정렬
- java
- 자바
- lambda
- 위클리 챌린지
- 추석맞이 코딩챌린지
- 프로그래머스
- Zip
Archives
- Today
- Total
상상쓰
[프로그래머스] 보석 쇼핑 본문
https://programmers.co.kr/learn/courses/30/lessons/67258
코딩테스트 연습 - 보석 쇼핑
["DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"] [3, 7]
programmers.co.kr
start 와 end 를 0 으로 잡고 조건을 잘 설정하여 1 씩 늘려준다. 모든 보석을 포함하는 구간을 만족했을 때 비교하여 길이가 최소가 되는 구간을 찾는다.
from collections import defaultdict
def solution(gems):
answer = []
N = len(set(gems))
dic = defaultdict(int)
dic[gems[0]] = 1
start, end = 0, 0
m = len(gems) + 1
while start < len(gems) and start <= end:
if N != len(dic):
end += 1
if end == len(gems):
break
dic[gems[end]] += 1
else:
if N == len(dic) and m > end - start:
m = end - start
answer = [start+1, end+1]
dic[gems[start]] -= 1
if dic[gems[start]] == 0:
del dic[gems[start]]
start += 1
return answer
print(solution(['DIA', 'RUBY', 'RUBY', 'DIA', 'DIA', 'EMERALD', 'SAPPHIRE', 'DIA'])) # [3, 7]
'Coding Test' 카테고리의 다른 글
[프로그래머스] 불량 사용자 (0) | 2021.06.08 |
---|---|
[프로그래머스] 경주로 건설 (0) | 2021.06.07 |
[백준] 단어 수학 (0) | 2021.06.06 |
[백준] 신입 사원 (0) | 2021.06.06 |
[백준] 잃어버린 괄호 (0) | 2021.06.05 |
Comments