-
[프로그래머스] 프린터 - 파이썬(Python)개발/코딩테스트 2022. 6. 20. 19:44
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
My Solution
def solution(priorities, location): answer = 0 while priorities: item = priorities.pop(0) if len(priorities) == 0: answer += 1 break else: if item < max(priorities): priorities.append(item) else: answer += 1 if location == 0: break if location == 0: location = len(priorities) - 1 else: location -= 1 return answer Key Idea
- location의 위치를 계속 update 해주면서 관리
- max를 이용한 현재 우선순위 파악
Best Solution
def best(priorities, location): queue = [(i, p) for i, p in enumerate(priorities)] answer = 0 while True: cur = queue.pop(0) if any(cur[1] < q[1] for q in queue): queue.append(cur) else: answer += 1 if cur[0] == location: return answer Key Idea
- 2번 line에 enumerate로 queue를 만들어 target의 현재 위치를 관리할 필요 X
- any를 이용 → 한개라도 True 라면 True 반환
'개발 > 코딩테스트' 카테고리의 다른 글
[프로그래머스]베스트앨범 - 파이썬(Python) (0) 2022.07.02 [프로그래머스]소수 찾기 - 파이썬(Python) (0) 2022.06.30 [프로그래머스]가장 큰 수 - 파이썬(Python) (0) 2022.06.30 [프로그래머스]디스크 컨트롤러 - 파이썬(Python) (0) 2022.06.29 [프로그래머스]전화번호 목록 - 파이썬(Python) (0) 2022.06.20