-
프로그래머스 42587 프로세스 (queue, priorityQueue)알고리즘 공부 2023. 5. 13. 21:36
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr

1. 배열을 순서대로 꺼내서 비교할 일반적인 큐가 필요하고,
2. 우선순위가 제일 높은 프로세스인지 비교할 우선순위 큐가 필요하다.
3. location 에 대한 값 판단에 유의해야함.
- 큐에 넣어서 빼고 더하는 순간 찾아야하는 프로세스 위치가 유동적으로 바뀌므로 그에 따라 location 도 유동적으로 바꿔줘야한다
- 찾아야할 프로세스가 맨 앞이 아닌 이상 1씩 줄어들 것이고, 맨 앞인데 우선순위가 아닐 경우는 맨뒤로 갈 것임
import java.util.*; class Solution { public int solution(int[] priorities, int location) { int answer = 0; PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); Queue<Integer> q = new LinkedList<>(); for(int i=0; i< priorities.length; i++){ pq.offer(priorities[i]); q.offer(priorities[i]); } int cnt =0; while(!q.isEmpty()){ int qnum = q.poll(); if(qnum==pq.peek()){ cnt ++; pq.poll(); if(location==0) return cnt; location --; }else{ location = (location!=0) ? location-1 : pq.size()-1; q.offer(qnum); } } return answer; } }'알고리즘 공부' 카테고리의 다른 글
프로그래머스 12900 2xn 타일링 (피보나치 수열) (0) 2023.05.14 프로그래머스 42626 더 맵게 (PriorityQueue) (0) 2023.05.14 프로그래머스 42746 가장 큰 수 (배열의 Sort 재정의) (0) 2023.05.13 프로그래머스 154539 뒤에 있는 큰 찾기 (0) 2023.05.13 프로그래머스 178870 연속된 부분 수열의 합 (0) 2023.05.13