티스토리 뷰

def solution(priorities, location):
    answer = 0
    order = [0]*len(priorities)
    order[location] = 1

    while 1:
        m = max(priorities)
        num = priorities.pop(0)
        ord = order.pop(0)
        if num >= m and ord == 1:
            answer += 1
            return answer
        if num >= m and ord == 0:
            answer += 1
            continue
        else:
            priorities.append(num)
            order.append(ord)

order라는 리스트를 모두 0으로 만들어준다.

location은 찾아야할 인덱스가 있는 것 이므로 order[location] = 1로 해준다. 그러면 찾을 인덱스가 아닌 곳의 값들은 0이다.

이제 무한루프를 들어간다.

먼저 가장 큰 값을 m에 뽑아놓는다.

num 변수에 priorities리스트 맨 앞 값을 뽑아놓는다.

ord 변수에도 order리스트 맨 앞 값을 뽑아 놓는다.

만약 num이  num >= m이고 ord값도 1이라면 프린트해준 것이므로 answer +=1 해주고 return answer를하여 종료해준다.

만약 num이  num >= m이고 ord값도 0이라면 프린트해준 것이므로 answer +=1 해준다.

이외라면 프린트할 수 없으므로 priorities.append(num), order.append(ord)를 하여 리스트 뒤에 붙여준다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
글 보관함