[프로그래머스 / Level2] 프린터 (Java)
문제 보기
풀이
- 대기목록을 구현하기 위하여 FIFO인 Queue 사용
- location과 priority를 같이 저장하기 위하여 Map 사용
- 가장 우선순위가 높은지 비교하기 위해 isHighestPriority() 메소드 구현
class Solution {
public int solution(int[] priorities, int location) {
Map<Integer, Integer> map = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < priorities.length; i++) {
map.put(i, priorities[i]);
queue.add(i);
}
int answer = 1;
while(true) {
int front = queue.poll();
int frontPriority = map.get(front);
if(isHighestPrioriy(map, frontPriority)) {
if(front == location) break;
map.remove(front);
answer++;
} else {
queue.add(front);
}
}
return answer;
}
public boolean isHighestPrioriy(Map<Integer, Integer> map, int priority) {
boolean ret = true;
if (map.values().stream()
.filter(v -> v > priority)
.count() > 0) {
ret = false;
}
return ret;
}
}
Author And Source
이 문제에 관하여([프로그래머스 / Level2] 프린터 (Java)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@pppp0722/프로그래머스Level2프린터저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)