티스토리 뷰

728x90
반응형
문제 링크

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

 

코딩테스트 연습 - 기능개발

프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다. 또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는

programmers.co.kr

문제 풀이

큐를 이용해서 푸는 문제입니다. 

 

풀이순서.

1. 개발 진도를 먼저 Queue에 넣습니다.

2. 반복문을 돌리며 날짜(day 변수)를 증가시킵니다.

3-1. (Queue에서 뽑아져 나오는 진행도 + speed * day수가 100을 넘을시) TreeMap에 key는 day로 하여 해당 날짜에 배포될 개수를++합니다.

3-2 넘지 않을시 day를 ++하고 2에서부터 반복합니다.

4. map에 넣은 개수가 작업의 개수를 넘을시 반복문을 종료합니다.

5. TreeMap이므로 key값(day)으로 정렬되어있고 배포될개수가 저장되어있습니다.

6. TreeMap을 iterator를 사용하여 answer배열에 저장하고 return합니다.

소스 코드
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        
        Queue<Integer> que = new LinkedList();
        TreeMap<Integer,Integer> ans= new TreeMap<>();
        for (int prog : progresses) {
        	que.add(prog);
		}

        int day=1;
        int cnt=0;
        while(true) {

        	int prog = que.peek();

        	if(prog + speeds[cnt]*day >= 100) {
        		que.poll();
        		cnt++;
        		ans.put(day, ans.getOrDefault(day, 0)+1);
        	}else {
        		day++;
        	}
        	if(cnt>=speeds.length) break;
        }
        int[] answer = new int [ans.size()];
        
        Iterator<Integer> it = ans.keySet().iterator();
        int i = 0;
        while(it.hasNext()) {
        	answer[i++] = ans.get(it.next());
        }
        
        return answer;
    }
}
728x90
반응형
250x250
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함