프로그래머스 - 프로세스 (Level 2)
2024. 2. 26. 20:11ㆍ코딩테스트 정리(자바)
728x90
객체 생성할 생각을 못하고
맵으로 낑낑대며 풀어봤지만 실패..
import java.util.*;
class Solution {
class Process{
int loca=0; //인덱스
int pri=0; //우선순위
public Process(int loca,int pri){
this.loca=loca;
this.pri=pri;
}
}
public int solution(int[] priorities, int location) {
int answer = 0;
Queue <Process> q=new LinkedList<>();
for(int i=0;i<priorities.length;i++){
q.add(new Process(i,priorities[i]));
}
while(!q.isEmpty()){
Process process=q.peek();
int max=process.pri;
Boolean flag=true; //가장 우선순위가 높은 애였다면 true로
for(Process p:q){
if(max<p.pri){
flag=false; //우선순위가 높은 애가 있다면 false로
q.remove(); //제거 후 큐에 삽입
q.add(process);
break;
}
}
if(flag){
answer++;
if(process.loca==location){
return answer;
}
q.remove();
}
}
return answer;
}
}
1. for문으로 큐의 원소도 접근 가능하다는 것이 놀랍다
2. class 자유자재로 사용할 줄 알아야 한다
-> 필요하면 객체 만들기
728x90
'코딩테스트 정리(자바)' 카테고리의 다른 글
| 백준 - 입력 받기 (0) | 2024.02.27 |
|---|---|
| 프로그래머스 - 최소직사각형 (Level 1) (0) | 2024.02.27 |
| 프로그래머스 - 다리를 지나는 트럭 (Level 2) (0) | 2024.02.22 |
| 프로그래머스 - 기능개발 (Level 2) (1) | 2024.02.21 |
| 프로그래머스 - 주식가격(Level 2) (0) | 2024.02.20 |