-
Programmers LV.1 K번째수Programmers 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로 하는 작업은 너무 쉬웠다.
'Programmers' 카테고리의 다른 글
Programmers LV.1 모의고사 (0) 2020.12.30 Programmers LV.2 가장 큰 수 (0) 2020.12.29 Programmers LV.3 디스크 컨트롤러 (0) 2020.12.29 Programmers LV.2 더 맵게 (0) 2020.12.28 Programmers LV.2 프린터 (0) 2020.12.27