본문 바로가기
ALGORITHM/Queue

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

by sjs_2215 2019. 6. 24.

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

[기능 개발]

  • 문제 재정의:

작업의 진도가 적힌 정수 배열 progresses. input

각 작업이 하루에 % 할 수 있는지 개발 속도가 적힌 speeds 배열. input

뒷 기능이 먼저 끝난다해도 앞 기능이 끝날 때 같이 배포됨.


  • 생각한 것:

1.몇일 후에 배포 가능한지 계산해서 순서대로 새로운 저장공간 a에 저장.

2.a에 저장되어 있는 값들 & 큐를 이용해 계산

    - a[0]을 일단 pivot으로

   - 그 다음 값이 a[0]보다 작거나 같을때까지 count++   (count는 초기값이 1)

   - 다음 값이 a[0]보다 크다면  큐(temp)에 count를 집어넣어줌.

   - 그리고 a[0]이었던 pivot도 a[i]값으로 바꿔주고, count를 다시 1로 초기화 해줌

   - 마지막으로, a 배열의 맨 마지막 원소가 다음날에 배포될 경우도 처리해줘야 함

     > 예를 들어, a의 배열이 9, 2, 1, 17, 8, 20 일 때
     >
     > 20전까진 for문으로 돌며 다 해결이 되는데 20에 대한 것도 해결해야 함. 
     >
     > 20은 3번째 배포되며 기능은 1개이므로 answer[2]=1를 해줘야함. 

     => 그래서 for문 다 돌고 마지막으로 큐(temp)에 count를 집어넣어줌

이클립스 INPUT

3 93 30 55
3 1 30 5


  • 코드:
  1. 프로그래머스 코드
//프로그래머스 코드
import java.util.LinkedList;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int pivot=0;
        int z=0,count=1;

        int[] a=new int[progresses.length];
        LinkedList temp = new LinkedList();//answer에 집어넣을 값들을 저장해둔 스택

        a=work(progresses,speeds);//일수 계산한 것 저장
        pivot=a[0];
        count=1;

        for(int i=1;i<a.length;i++){
            if(pivot>=a[i]){
                count++;
            }
            else{
                temp.offer(count);//정답을 저장해둠.
                pivot=a[i];//기준일자를 변경
                count=1;
            }
        }
        temp.offer(count);

        int[] answer=new int[temp.size()];//정답 배열

        while(!temp.isEmpty()){
            answer[z]=(int)temp.poll();
            z++;
        }

        return answer;
    }

    public static int[] work(int[] x, int[] y) { //몇일 후 일 가능한지 계산    
        int[] check=new int[x.length] ;
        for(int i=0;i<x.length;i++){
            if((100-x[i])%y[i]==0){
                check[i]=(100-x[i])/y[i];
            }
            else{//나눠떨어지지 않는다면 일수를 +1해줌
                check[i]=(100-x[i])/y[i]+1;
            }
        }
        return check;
     }
}
  1. 이클립스 코드
//이클립스 코드
//BufferedReader 사용
package till;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        int n=Integer.parseInt(br.readLine());
        int[] arrays = new int[n];
        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
        for (int i = 0; i < n; i++) {
            // 배열에다 토큰을 하나씩 불러서 입력해줌
            arrays[i] = Integer.parseInt(st.nextToken());
        }     

        int n2=Integer.parseInt(br.readLine());
        int[] arrays2 = new int[n2];
        st = new StringTokenizer(br.readLine()); //공백 기준으로 나눔
        for (int i = 0; i < n2; i++) {
            // 배열에다 토큰을 하나씩 불러서 입력해줌
            arrays2[i] = Integer.parseInt(st.nextToken());
        }

        solution(arrays,arrays2);
    }

    public static int[] solution(int[] progresses, int[] speeds) {
        int pivot=0;
        int z=0,count=1;

        int[] a=new int[progresses.length];
        LinkedList temp = new LinkedList();//answer에 집어넣을 값들을 저장해둔 스택


        a=work(progresses,speeds);//일수 계산한 것 저장
        pivot=a[0];
        count=1;

        for(int i=1;i<a.length;i++){
            if(pivot>=a[i]){
                count++;
            }
            else{
                temp.offer(count);//정답을 저장해둠.
                pivot=a[i];//기준일자를 변경
                count=1;
            }
        }
        temp.offer(count);
        System.out.println(temp);

        int[] answer=new int[temp.size()];//정답 배열

        while(!temp.isEmpty()){//큐에 있던 것을 answer배열에 옮김
            answer[z]=(int)temp.poll();
            z++;
        }

        for(int ab:answer){
            System.out.println(ab);
        }
        return answer;
    }


    public static int[] work(int[] x, int[] y) { //몇일 후 일 가능한지 계산    
        int[] check=new int[x.length] ;
        for(int i=0;i<x.length;i++){
            if((100-x[i])%y[i]==0){
                check[i]=(100-x[i])/y[i];
            }
            else{//나눠떨어지지 않는다면 일수를 +1해줌
                check[i]=(100-x[i])/y[i]+1;
            }
        }
        return check;
     }

}

Comments