Programmers

Programmers LV.1 K번째수

Developer Garam.Choi 2020. 12. 29. 21:10
@org.junit.Test
    public void kArray(){

        int[] array = {1, 5, 2, 6, 3, 7, 4};
        int[][] commands = { {2,5,3},{4,4,1},{1,7,3}};



        List<Integer> answerList = new ArrayList<>();

        for(int[] cmd : commands){
            List<Integer> addList =new ArrayList<>();
            //a 배열자르기 첫번째
            //b 배열자르기 마지막째
            //c  c번째 값
            int cutFirst = cmd[0];
            int cutLast = cmd[1];
            int whichValue = cmd[2];

            for(int a=cutFirst-1; a< cutLast; a++){
                addList.add(array[a]);
            }

            Collections.sort(addList, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return Integer.compare(o1,o2);
                }
            });

            answerList.add(addList.get(whichValue-1));
        }

        int[] answer = new int[answerList.size()];

        for(int i=0; i<answerList.size(); i++){
            answer[i] = answerList.get(i);
            System.out.println(answer[i]);
        }

    }

큐스택이 아닌 Array로 하는 작업은 너무 쉬웠다.