곽로그

[프로그래머스 level2] 기능개발 본문

알고리즘/프로그래머스

[프로그래머스 level2] 기능개발

일도이동 2020. 12. 3. 22:49
반응형

문제

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

 

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

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

programmers.co.kr

 

풀이1 

방법

1) 변수 

- skillDeploy : 배포할지 말지 검사하는 큐 

- skillDevelop :스피드 만큼 진행하는 큐

 

2) 초기화

- skillDeploy에 progresses순서대로 add 한다

 

3) 반복

- skillDeploy의 맨 위의 스킬의 진행도를 확인한다

- 100 미만이면 skillDeploy에서 하나씩 꺼내서 스피드만큼 진행시킨다음 skillDevelop큐에 넣는다. 진행이 끝났으면 다시 skillDeplop에 add한다

- 100 이상이면 100이상인 스킬을 poll한다. 이때 poll한 갯수가 그 회차에 배포를 한 스킬의 갯수다 

 

 

 

실수한 것

queue.poll()을 할때 queue가 비어있는지 먼저 확인해야한다.

 

코드

import java.util.*;
class Skill{
    int progress;
    int speed;

    Skill(int progress, int speed){
        this.progress = progress;
        this.speed = speed;
    }
}
class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] answer = {};
        
        Queue<Skill> skillDeploy = new LinkedList<>();          //배포 할 수 있는지 검사하는 스택
        Queue<Skill> skillDevelop = new LinkedList<>();    // 기능 개발하는 스택
        ArrayList<Integer> deployCount = new ArrayList<>();

        for(int index = 0; index<progresses.length; index++){
             Skill skill = new Skill(progresses[index],speeds[index]);
             skillDeploy.add(skill);
        }

        while(!skillDeploy.isEmpty()){
            //가장 먼저 배포되야할 스킬의 진행율이 100 미만이면, 각 스킬을 스피드 만큼 진행

            if(skillDeploy.peek().progress<100){
                System.out.println(skillDeploy.peek().progress);
                while(!skillDeploy.isEmpty()){
                    Skill toBeDeveloped= skillDeploy.poll();
                    int nextProgress = toBeDeveloped.progress + toBeDeveloped.speed;
                    int speed = toBeDeveloped.speed;
                    skillDevelop.add(new Skill(nextProgress, speed));
                }

                while(!skillDevelop.isEmpty()){
                    Skill toBeDeveloped = skillDevelop.poll();
                    skillDeploy.add(toBeDeveloped);
                }
            }
            else{
                int count = 0;
                while(!skillDeploy.isEmpty() && skillDeploy.peek().progress>=100){
                    skillDeploy.poll();
                    count++;
                }
                deployCount.add(count);
            }
        }

        answer = new int[deployCount.size()];
        for(int index = 0 ; index< deployCount.size();index++){
            answer[index] = deployCount.get(index);
        }

        
        return answer;
    }
}

 

풀이2

방법

1. 현재 스킬의 진행도가 100이 될때까지 남은 일 계산  (100- 현재스킬을 스피드로 나눈 몫. 이때 나머지가 있으면 +1)

2. 계산한 일 수를 Queue에 add한다

 

3. 맨 위에 있는 요소를 poll 한다 (배포우선순위가 가장 큰 스킬이 배포까지 남은 날)

4. poll한 요소보다 큰 수가 나올 때 까지 poll한다 ( 배포우선순위가 가장 큰 스킬의 배포까지 남은 날 보다 적게 남은 스킬을 poll) 

5. poll한 갯수를 count 한다 (한 회차에 함께 배포할 수 있는 스킬의 수)

6. count를 List에 add한다 

 

6. queue가 비어있지 않을때까지 반복한다. 

 

실수한 것

1) 남은 진행도를 구할 때 나머지가 있는 경우 +1을 해줘야 한다

2) daysToComplete.peek()<firstDeploy 가 아닌 daysToComplete.peek()<=firstDeploy

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

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] answer = {};
        
       //완성할 때까지 걸리는 시간 게산
        Queue<Integer> daysToComplete = new LinkedList<>();
        for(int index = 0 ; index<progresses.length; index++){
            int currentProgress = progresses[index];
            int speed = speeds[index];
            int remainingProgress = 100 - currentProgress;
            int remainingDays =  remainingProgress % speed >0 ? remainingProgress /speed +1 : remainingProgress /speed;
            daysToComplete.add(remainingDays);
        }


        ArrayList<Integer> deployCount = new ArrayList<>(); // 한 회차에 배포하는 스킬의 갯수
        while(!daysToComplete.isEmpty()){
            int count = 0;

            //먼저 배포해야하는 스킬을 poll한다
            int firstDeploy = daysToComplete.poll();
            ++count;

            // 먼저 배포해야하는 스킬보다 이전에 완성된스킬을 poll한다
            while(!daysToComplete.isEmpty() && daysToComplete.peek()<=firstDeploy){
                daysToComplete.poll();
                ++count;
            }

            // poll한 갯수가 이번 회차에 배포하는 스킬의 갯수다
            deployCount.add(count);
        }
        
        //answer
        answer = new int[deployCount.size()];
        for(int index = 0 ; index<answer.length; index++){
            answer[index] = deployCount.get(index);
        }
        
        
        return answer;
    }
}

 

코드

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

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] answer = {};
        
       //완성할 때까지 걸리는 시간 게산
        Queue<Integer> daysToComplete = new LinkedList<>();
        for(int index = 0 ; index<progresses.length; index++){
            int currentProgress = progresses[index];
            int speed = speeds[index];
            int remainingProgress = 100 - currentProgress;
            int remainingDays =  remainingProgress % speed >0 ? remainingProgress /speed +1 : remainingProgress /speed;
            daysToComplete.add(remainingDays);
        }


        ArrayList<Integer> deployCount = new ArrayList<>(); // 한 회차에 배포하는 스킬의 갯수
        while(!daysToComplete.isEmpty()){
            int count = 0;

            //먼저 배포해야하는 스킬을 poll한다
            int firstDeploy = daysToComplete.poll();
            ++count;

            // 먼저 배포해야하는 스킬보다 이전에 완성된스킬을 poll한다
            while(!daysToComplete.isEmpty() && daysToComplete.peek()<=firstDeploy){
                daysToComplete.poll();
                ++count;
            }

            // poll한 갯수가 이번 회차에 배포하는 스킬의 갯수다
            deployCount.add(count);
        }
        
        //answer
        answer = new int[deployCount.size()];
        for(int index = 0 ; index<answer.length; index++){
            answer[index] = deployCount.get(index);
        }
        
        
        return answer;
    }
}
반응형
Comments