[Programmers]-기능개발(스택/큐)
문제
“코딩테스트 고득점 Kit - 스택/큐” 문제이다.
큐로 풀기 적합한거 같아 큐로 풀었다.
코드
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
class Solution {
public class Pro{
int progress;
int speed;
public Pro(int progress, int speed){
this.progress = progress;
this.speed = speed;
}
}
public int[] solution(int[] progresses, int[] speeds) {
int len = speeds.length;//길이
Queue<Pro> q = new LinkedList<>();//큐에 데이터 담기
for(int i=0;i<len;i++){
q.offer(new Pro(progresses[i],speeds[i]));
}
ArrayList<Integer> list = new ArrayList<>();
int count = 0;//완료된 수
while(!q.isEmpty()){
for(Pro p : q){//각 speed 더해주기
p.progress += p.speed;
}
while(!q.isEmpty()){
Pro k = q.peek();
if(k.progress >= 100){//완료된 조건
q.remove();
count++;//완료 수 증가
}
else break;
}
if(count != 0)
list.add(count);
count = 0;
}
int[] answer = new int[list.size()];
for(int i=0;i<list.size();i++)
answer[i] = list.get(i);
return answer;
}
}
코드 설명
Pro객체 생성.
큐에 객체 담아 핸들링.
한 사이클마다 큐전체에 각speed더해주기.
progress가 100이상이면 완료 조건.
count(완료된 progress수)를 ArrayList로 컨트롤.
요약
-
offer : 데이터 추가
-
peek : 데이터 조회
-
poll : 데이터 조회,삭제