본문 바로가기

algorithm/프로그래머스

프로그래머스_기능개발(C++, JAVA)

 

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

 

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

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

programmers.co.kr

[문제 풀이]

queue을 이용하여 문제를 풀었습니다.

이 문제는 함께 배포될 수 있는 작업의 수를 각각 구하는 문제입니다. 작업이 배포되기 위해서는 아래의 조건을 만족하여야 합니다.

  1. 진행 중인 작업 + 작업 속도 * 작업일 >= 100을 만족해야 합니다.
  2. 완료되지 않은 작업이 앞에 존재하지 않아야 합니다. 
  3. 완료된 작업 중 배포되지 않은 작업이 뒤이어 존재한다면(연속적) 뒤의 작업을 함께 배포합니다.

로직을 설명해 드리겠습니다.

  1. 작업이 완료되는데 걸리는 시간을 큐에 삽입합니다.
  2. 큐의 앞의 숫자를 기준으로 기준보다 큰 숫자가 나올 때까지 완료된 작업 수를 상승시켜줍니다.
  3. 기준보다 큰 수가 나온다면 작업 수를 저장시키고 초기화시켜줍니다.
  4. 큐가 빌 때까지 반복합니다.

[C++]

#include <string>
#include <vector>
#include <queue>
#include<iostream>


using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    queue<int> que; 
    
    for(int i = 0; i < progresses.size(); i++){
        int time = ((100 - progresses[i]) % speeds[i] > 0) ? 
            (100 - progresses[i]) / speeds[i] + 1 : (100 - progresses[i]) / speeds[i];
        
        que.push(time);
    }

    
    while(!que.empty()){
        int day = 1;
        int front = que.front();
        que.pop();
        
        while(!que.empty()){
            if(que.front() <= front){
                day++;
                que.pop();
            }else
                break;
        }
        answer.push_back(day);
    }
    
    
    return answer;
}

 

[JAVA]

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Solution{
    public int[] solution(int[] progresses, int[] speeds) {
    	int[] answer = {};

        ArrayList<Integer> answerList = new ArrayList<>();
    	Queue<Integer> que = new LinkedList<>();
        
    	for(int i = 0; i < progresses.length; i++) {
    		int time = ((100 - progresses[i]) % speeds[i] > 0) ? 
    	            (100 - progresses[i]) / speeds[i] + 1 : (100 - progresses[i]) / speeds[i];
    		
    		que.add(time);
    	}
        
    	
    	while(!que.isEmpty()) {
    		int count = 1;
    		int before = que.poll();
    		
    		while(!que.isEmpty()) {
    			if(que.peek() <= before) {
    				count++;
    				que.poll();
    			}else
    				break;
    		}
    		answerList.add(count);
    	}
    	
    	answer =  answerList.stream().mapToInt(Integer::intValue).toArray();
        
        return answer;
    }
}