Algorithm/프로그래머스
[프로그래머스] 프린터(자바) / 자료구조
미스터로즈
2021. 9. 9. 09:34
문제 설명
제안사항 및 입출력
문제 풀이
import java.util.*;
class Solution {
static class task{
int prior;
int loc;
public task(int prior,int loc){
this.prior=prior;
this.loc=loc;
}
}
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<task> q = new LinkedList<>();
for(int i = 0 ; i < priorities.length ; i++){
q.add(new task(priorities[i],i));
}
while(true){
task tmp = q.poll();
boolean flag = false;
for(int i = 0 ; i < q.size();i++){
task sub = q.poll();
if(sub.prior>tmp.prior){
flag=true;
}
q.add(sub);
}
if(flag==false){
answer++;
if(tmp.loc==location){
break;
}
}else{
q.add(tmp);
}
}
return answer;
}
}
※ 내 생각
이 문제는 자료구조 중 큐를 이용하면 쉽게 해결할 수 있었습니다.
먼저 작업에 대한 중요성과 위치에 대한 정보를 담는 클래스를 만들어줍니다.
큐에 만들어준 클래스를 담아줍니다.
while를 통해서 원하는 위치의 값이 나올수 있는지를 찾아줍니다.
만약 나올 수 없다면 다시 넣어주는 작업을 반복해줍니다.