티스토리 뷰

제한사항

  • array의 길이는 1 이상 100 이하입니다.
  • array의 각 원소는 1 이상 100 이하입니다.
  • commands의 길이는 1 이상 50 이하입니다.
  • commands의 각 원소는 길이가 3입니다.

 

 

중요한 건 Commands에 "모든" 배열에 대하여 각각 연산을 하고, 리턴 값을 받아야 한다는 것.

이 점만 주의하면, 딱히 신경쓸 건 없어보이는 쉬운 문제이다.


배열과 i, j를 받아 우선 받은 배열을 자르는 cutter 메서드를 추가하고, commands의 개수만큼 cuuter로 자른 결과 배열을 정렬, 그 후 찾는 위치에 있는 값을 출력하도록 작성했다.


package com.choonham;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public  List<Integer> solution(int[] array, int[][] commands) {
        List<Integer> answer = new ArrayList<>();
        
        for(int k = 0; k < commands.length; k++) {
        	int[] temp = cutter(array, commands[k][0], commands[k][1]);
        	Arrays.sort(temp);
        	answer.add(temp[commands[k][2] - 1]);
        }
        
        return answer;
    }
    public int[] cutter(int[] array, int i, int j) {
    	int[] result = new int[ j - (i-1)];
    	int temp = i;
    	for(int k = 0; k < result.length; k++) {
    		result[k] = array[temp-1];
    		temp++;
    	}
    	return result;
    }
}

어려울 거 없는, 기본에 충실한 문제였다. 

 

 

끝!

Comments