문제 설명
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
- array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
- 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
- 2에서 나온 배열의 3번째 숫자는 5입니다.
배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
제한사항
- array의 길이는 1 이상 100 이하입니다.
- array의 각 원소는 1 이상 100 이하입니다.
- commands의 길이는 1 이상 50 이하입니다.
- commands의 각 원소는 길이가 3입니다.
입출력 예
[1, 5, 2, 6, 3, 7, 4] | [[2, 5, 3], [4, 4, 1], [1, 7, 3]] | [5, 6, 3] |
입출력 예 설명
[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.
< 나의 풀이 1 >
import java.util.ArrayList; class Solution { public int[] solution(int[] array, int[][] commands) { ArrayList<Integer> tempArr = new ArrayList<>(); // 임시 ArrayList<Integer> answerArr = new ArrayList<>(); // 전체 반복문 필요(commands의 length만큼) => answerArr에 순서대로 add for (int i = 0; i < commands.length; i++) { tempArr.clear(); int[] tempAnswerInFor = {}; int temp = 0; // 1) commands의 i,j 이용하여 tempArr에 add 하기 for (int j = commands[i][0] - 1; j <= commands[i][1] - 1; j++) { tempArr.add(array[j]); } // end of for_j // 2) tempArr하나가 완성되면 배열로 저장하여 ->정렬-> answerArr에 순서대로 넣어주기 // - tempArr하나가 완성되고, 배열에 넣어줌 tempAnswerInFor = new int[tempArr.size()]; for (int k = 0; k < tempArr.size(); k++) { tempAnswerInFor[k] = tempArr.get(k); } // - 배열 정렬 for (int l = 0; l < tempAnswerInFor.length; l++) { for (int m = 0; m < tempAnswerInFor.length - 1 - l; m++) { if (tempAnswerInFor[m] > tempAnswerInFor[m + 1]) { temp = tempAnswerInFor[m]; tempAnswerInFor[m] = tempAnswerInFor[m + 1]; tempAnswerInFor[m + 1] = temp; } } } // - answerArr에 넣어주기 answerArr.add(tempAnswerInFor[commands[i][2]-1]); } // end of for_i (전체 for) int[] answer = new int[answerArr.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = answerArr.get(i); } // 테스트코드 for (int i = 0; i < answer.length; i++) { System.out.println(answer[i]); } return answer; } } |
ArrayList를 이 문제를 풀면서 처음 알게됐고 첫 활용이였다.
아직 정확한 활용법은 모르는 것 같지만 어찌됐든 문제는 풀었음. 헥헥
< 나의 풀이 2 (19/4/24 추가) > - 20days later from first answer.
public class test_2 { public static int[] solution(int[] array, int[][] commands) { return answer; |
collections.sort기능 활용함.
보완점 : Arrays sort를 활용하면 정렬기능과 copy기능을 사용할 수 있다.
< 다른 사람의 풀이 >
import java.util.Arrays; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; for(int i=0; i<commands.length; i++){ int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]); Arrays.sort(temp); answer[i] = temp[commands[i][2]-1]; } return answer; } } |
for(int i=0; i<commands.length; i++){
반복문은 2차원 배열 commands의 행 길이(=3)만큼 반복하게 짜여져있다.
(열길이의 length는 commands[i].length 이런식으로 했던 듯)
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
: Arrays클래스의 copyOfRange는 활자 그대로 범위를 copy하는 메소드겠다.
* Arrays.copyOfRange(복사할 배열, 복사범위 시작, 복사범위 끝(끝은 포함 안됨.그 전까지만));
위와 같은 식으로 활용한다.
따라서 풀이에서는 배열 array의 commands[i][0]-1(=array[1]=5(*첫번째case)) 부터
commands[i][1](array[4]=3) 까지 임시배열 temp에 복사한다는 의미.
그리고 Arrays의 sort메소드로 자동정렬.
answer[i] = temp[commands[i][2]-1];
그리고 answer[i]에 (예제의 반복문은 총 3번 반복될 터이니 answer[0],[1],[2] 값에 차례대로 들어간다.)
임시배열 temp의 commands[i][2]-1넣어주기(3-1이니까 temp[2]값이 들어간다. )
새로 알게 된 것
- ArrayList
- Arrays 클래스의 copyOfRange 메소드, sort 메소드
'Tech > Algorithm' 카테고리의 다른 글
프로그래머스)level.1_소수 찾기(에라토스테네스의 체) (0) | 2019.04.04 |
---|---|
프로그래머스)level.1_하샤드 수 (0) | 2019.04.03 |
프로그래머스)level.1_완주하지 못한 선수(hash 공부하고 쓸것) (0) | 2019.04.03 |
프로그래머스)level.1_모의고사 (0) | 2019.04.03 |
프로그래머스)level.1_문자열 다루기 기본 (0) | 2019.04.03 |
댓글