GenieLove! 2021. 3. 14. 00:15
728x90
반응형

Java

import java.util.*;
class Solution {
    public int solution(int[] priorities, int location) {//location은 priorities의 인덱스
        LinkedList<Integer> list = new LinkedList<Integer>();//작업 큐
        List<Integer> listSort = new ArrayList<Integer>();//작업 큐 정렬된 리스트
        
        for(int i = 0; i < priorities.length; i++){
            list.add(priorities[i]);
            listSort.add(priorities[i]);
        }
        
        Collections.sort(listSort);
        int order = 1;//location이 진행될 순서
        while(true){
            if(list.get(0) == listSort.get(listSort.size() - 1)){//list의 인덱스 0번째의 중요도와 정렬한 것의 제일 큰 중요도가 같으면
                if(location == 0)  return order;//찾는 인덱스와 같으면
                else{
                    order++;
                    location = location - 1 < 0 ? list.size() - 1 : location - 1;
                    list.removeFirst();
                    listSort.remove(listSort.size() - 1);
                }
            }else{//list의 0번째 값을 마지막으로 변경
                list.add(list.remove());
                location = location - 1 < 0 ? list.size() - 1 : location - 1;
            }
        }

    }
}

Python

def solution(priorities, location):
    order = 1
    
    while True:
        if priorities[0] == max(priorities):# 현재 문서의 중요도가 제일 높은 거라면
            if location == 0:#요청한 문서 차례라면
                return order#반환!
            else:#요청한 문서 아니면 -> 리스트에서 삭제
                del priorities[0]#리스트에서 제일 앞 요소 삭제
                location = location - 1 if location - 1 >= 0 else len(priorities) - 1
                order += 1
        else:#현재 문서의 중요도가 제일 높은 값이 아니라면
            priorities.append(priorities.pop(0))
            location = location - 1 if location - 1 >= 0 else len(priorities) - 1
728x90
반응형