[Python] 프로그래머스 / level 2 / 알고리즘 고득점 Kit / 프로세스
2024. 3. 28. 08:22ㆍCoding Test/Python
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
from collections import deque
def solution(priorities, location):
answer = 0
max_p = max(priorities)
queue = deque([(i, p) for i, p in enumerate(priorities)])
while queue:
q = queue.popleft()
if queue and max_p > q[1]:
queue.append(q)
else:
answer += 1
if q[0] == location:
break
return answer
이렇게 할 경우 테케 2에서 시간초과가 발생한다...왜지....?
def solution(priorities, location):
answer = 0
queue = [(i, p) for i, p in enumerate(priorities)]
while queue:
process = queue.pop(0)
if any(process[1]<q[1] for q in queue):
queue.append(process)
else:
answer += 1
if process[0] == location:
return answer
가장 큰 우선순위가 맨 앞에 올때까지 if문을 돌린다음,
else 문을 통해 process[0]이 location 값이랑 같을 때까지 anwer에 1씩 더해 location이 몇 번째 실행되는지 알 수 있게 된다!
'Coding Test > Python' 카테고리의 다른 글
| [Python] 프로그래머스 / level 1 / 체육복 / 그리디 / 반례 (1) | 2024.03.29 |
|---|---|
| [Python] 프로그래머스 / level 4 / 무지의 먹방 라이브 / heapq / 그리디 (0) | 2024.03.29 |
| [알고리즘] 코딩테스트 알고리즘 문제 / 프로그래머스 (0) | 2024.03.22 |
| [Python] 프로그래머스 / 알고리즘 고득점 Kit / hash 정리 / items() (0) | 2024.03.20 |
| [Python] 힙(heapq) 정리 (0) | 2024.03.15 |