https://programmers.co.kr/learn/courses/30/lessons/42587?language=python3
- 1차 풀이 (flag 사용)
from collections import deque def solution(priorities, location): answer = [] stack = deque([i for i in range(0, len(priorities))]) priorities = deque(priorities) while priorities: flag = 0 for priority in priorities: if priority > priorities[0]: flag = 1 break else: priorities.popleft() answer.append(stack.popleft()) if flag: item = priorities.popleft() priorities.append(item) item = stack.popleft() stack.append(item) return answer.index(location) + 1
- 2차 풀이 (훨씬 간결하고 효율적인 로직)
def solution(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
'algorithm > python' 카테고리의 다른 글
프로그래머스/스택,큐/다리를 지나는 트럭 (0) | 2022.06.21 |
---|---|
프로그래머스/스택,큐/기능 개발 (0) | 2022.06.20 |
프로그래머스/스택,큐/주식 가격 (0) | 2022.06.14 |
[python] 최대 int 범위 (1) | 2022.03.27 |
codility/lesson5/prefix sums/passingCars (0) | 2022.02.14 |