[프로그래머스]기능개발(JAVA)

JESS YANG·2021년 5월 2일
0

프로그래머스

목록 보기
6/13
post-thumbnail

문제

https://programmers.co.kr/learn/courses/30/lessons/42586

내 코드

class Program{
	int progress;
	int speed;
	public Program(int progress, int speed) {
		this.progress = progress;
		this.speed = speed;
	}
}
public List<Integer> solution(int[] progresses, int[] speeds) {
	List<Integer> rlt = new ArrayList<>();
	//2
	Queue<Program> queue = new LinkedList<>();
	for(int i=0; i<progresses.length; i++) {
		queue.offer(new Program(progresses[i], speeds[i]));
	}
    	//3
	while(!queue.isEmpty()){
		int count = 0;
        	//4
		for(int i=0; i<queue.size(); i++) {
			Program temp = queue.poll();
			temp.progress += temp.speed;
			queue.offer(temp);
		}
		//5
		while(queue.size()>0) {
			if(queue.peek().progress >= 100) {
				queue.poll();
				count++;
			}else {
				break;
			}
		}
        	//6
		if(count >0)
			rlt.add(count);
		
	}
	
	return rlt;
}

풀이

  1. 작업진도와 작업속도를 생성자 파라미터로 받는 Program class 정의
  2. 반복문을 돌면서 큐에 program 객체를 담는다.
  3. 큐가 비워질 때까지 반복문 수행
  4. 프로그램의 진도를 반복문을 돌면서 작업속도 만큼 올려준다.
  5. 작업이 완료된 프로그램은 큐에서 빼주고 count를 증가시킨다.
  6. 증가된 count 수 만큼 결과 list에 입력해준다.

0개의 댓글