[프로그래머스] 프린터

박신희·2022년 4월 11일
0

[풀이] 프로그래머스

목록 보기
11/33
post-thumbnail

❗ 풀이 과정

  • 하나씩 pop 해서 비교하고 최댓값이 아니라면 다시 뒤로 집어넣는다.
  • pop 할때마다 location이 바뀌기 때문에 location을 계속 update 해준다.
  • 출력이 될 순서가 왔을 때, 우리가 프린트 해야할 출력물인지 확인해준다.

🤜 풀이 코드

def solution(priorities, location):
    pick_idx=location
    max_num=max(priorities)
    print_order=0
    while True:
        # 첫번째 요소를 pop
        tmp=priorities.pop(0)
        
        # 현재 location update
        if pick_idx>=0:
            pick_idx-=1
        else:
            pick_idx+=len(priorities)
            
        if max_num>tmp:
            # max값이 아니라면 뒤에 넘기기
            priorities.append(tmp)
        else:
            # 출력 순서 +1
            print_order+=1    
            # pick_idx가 -1 일 경우 == pop 됐을 경우 (max값이면서, pop된 경우)
            if pick_idx==-1:
                return print_order   
            # max 값 update
            max_num=max(priorities)
  • pick_idx가 -1인 경우는 우리가 출력해야 되는 게 pop이 된 상태이다.
profile
log my moments 'u')/

0개의 댓글