Algorithm/Stack, Queue

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

SalTae 2019. 11. 18. 21:44

비교적 가벼운 문제로 큐의 사용방법과 가벼운 이해도를 위한 문제이다.

다른건 크게 문제 없이 이해하고 넘어 갈 수 있는 부분이지만 연구실 생활중에 후배가 질문했던 부분에 대해 몇가지 정리하자면

((LinkedList<int[]>) WorkingProcess).add(new int[] {progresses[i],speeds[i]});

new int[] {progresses[i],speeds[i]} 이부분은 단순하게 배열을 생성해서 해당 배열을 Queue에 더하는 것이다.

     for(int i=0;i<progresses.length;i++){

            int[] tmp = {progresses[i],speeds[i]};

      ((LinkedList<int[]>) WorkingProcess).add(new int[] {progresses[i],speeds[i]});

      }

둘다 정상 동작이 가능하다.

int [] answer = compare.stream().mapToInt(i->i).toArray();

자바8버전 부터 Stream기능을 이용하여 편리하게 사용 할 수있는 부분이 많다 . 자바에서의 맵은 파이썬에서 딕셔너리와 같은 기능을 한다.

다르게 파이썬에서는 map이라는 고차함수 기능을 통해 편리하게 코딩하게 가능하게 해주는 ... 깊게 들어가지말자 자세한 내용은

JAVA Stream 사용법을 검색해서 해당 글을 보도록하자.

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

class Solution {
    public static int[] solution(int[] progresses, int[] speeds) {
    	//큐 생성 
        Queue<int[]>WorkingProcess = new LinkedList<>();
        //완료된 계산을 담을 배열생성 
        int[] result = new int[progresses.length];
        for(int i=0;i<progresses.length;i++){
        	//큐에 정보 입력 
            ((LinkedList<int[]>) WorkingProcess).add(new int[] {progresses[i],speeds[i]});

        }
        //완료된 정보의 위치를 담기위한 배열의 현재위치를 가리키는 result_count 
        int result_count = 0;
        while(!WorkingProcess.isEmpty()){
            int[] tmp = WorkingProcess.poll();
            
            int count=0;
            //100%가 완료 될 때까지 연산을 진행한다.
            while(tmp[0]<100){
                tmp[0]+=tmp[1];
                count++;
            }
           //완료된 정보를 result배열에 저장한다.
            result[result_count]=count;
            result_count++;

        }
        ArrayList<Integer>compare = new ArrayList<Integer>();
        //제일 처음 진행된 일의 값을 담는 변
        int flag = result[0];
        int count = 1;
        int tmp=0;
        
        for(int i=1;i<result.length;i++){
            tmp=result[i];
            if(flag<tmp){
                compare.add(count);
                flag=tmp;
                count=1;
                if(i==result.length-1){
                    compare.add(count);
                }
            }else{
                count++;
                if(i==result.length-1){
                    compare.add(count);
                }
            }
        }

        //return값이 int 형 배열이기 때문에 List를 int array로 변경해준다.
        int [] answer = compare.stream().mapToInt(i->i).toArray();
        

        return answer;
    }

    

}

비교적 가벼운 문제로 큐의 사용방법과 가벼운 이해도를 위한 문제이다.

다른건 크게 문제 없이 이해하고 넘어 갈 수 있는 부분이지만 연구실 생활중에 후배가 질문했던 부분에 대해 몇가지 정리하자면

((LinkedList<int[]>) WorkingProcess).add(new int[] {progresses[i],speeds[i]});

new int[] {progresses[i],speeds[i]} 이부분은 단순하게 배열을 생성해서 해당 배열을 Queue에 더하는 것이다.

     for(int i=0;i<progresses.length;i++){

            int[] tmp = {progresses[i],speeds[i]};

      ((LinkedList<int[]>) WorkingProcess).add(new int[] {progresses[i],speeds[i]});

      }

둘다 정상 동작이 가능하다.

int [] answer = compare.stream().mapToInt(i->i).toArray();

자바8버전 부터 Stream기능을 이용하여 편리하게 사용 할 수있는 부분이 많다 . 자바에서의 맵은 파이썬에서 딕셔너리와 같은 기능을 한다.

다르게 파이썬에서는 map이라는 고차함수 기능을 통해 편리하게 코딩하게 가능하게 해주는 ... 깊게 들어가지말자 자세한 내용은

JAVA Stream 사용법을 검색해서 해당 글을 보도록하자.