문제 : https://programmers.co.kr/learn/courses/30/lessons/42587
스택/큐 카테고리에 있는데, 그냥 큐를 쓰라는 문제같다.
하지만 귀찮고 + N이 100도 안나오고 + 답만 맞으면 된다는 트롤링급 문제풀이 방법에 따라 최적화 따윈 없는 끔직한 코드가 탄생했다.
푸는 방법은 다음과 같다.
- priorities 에서 맨 앞을 보고
- priorities[1:] 에서 맨 앞보다 중요도가 높은 원소가 있으면
- pop + append(파이썬 pop(0) 은 무려 O(N) 이다. ) 으로 뒤에 붙인 뒤 목표를 한칸 앞당긴다.
- priorities[1:] 에서 맨 앞보다 중요도가 높은 원소가 없으면
- 돌다보니 맨 앞 원소가 location 이었던 원소면 바로 답 반환
- 아니면 그냥 pop 후 배열, curloc 등을 1씩 앞당긴다.
- priorities[1:] 에서 맨 앞보다 중요도가 높은 원소가 있으면
def solution(priorities, location):
answer = 1
curloc = location%len(priorities)
myp = priorities[location]
pl = priorities
while True:
if pl[0] < max(pl):
t = pl[0]
pl.pop(0)
pl.append(t)
curloc = (curloc-1)%len(priorities)
else:
if pl[0] == myp and curloc==0:
return answer
else:
pl.pop(0)
answer+=1
curloc = (curloc-1)%len(priorities)
return answer
'취준 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 Programmers][Python] 문자열 압축 (0) | 2020.01.22 |
---|---|
[프로그래머스 Programmers][Python] 완주하지 못한 선수 (0) | 2020.01.20 |
[프로그래머스 Programmers][Python] 쇠막대기 (0) | 2020.01.18 |
[프로그래머스 Programmers][Python] 다리를 지나는 트럭 (0) | 2020.01.18 |
[프로그래머스 Programmers][Python] 탑 (0) | 2020.01.17 |