본문 바로가기
JAVA 코딩 알고리즘 연습/프로그래머스

프로그래머스 - K번째수 / JAVA (자바) 코딩 알고리즘 연습

by easpop 2022. 12. 15.
728x90
반응형
반응형

로직 짜는데 살짝 어려웠던..? 문제였음

for문 안에 list 배열을 만들어서 그 범위 안에서 정렬하고 answer 배열에 담고

그걸 반복하는 로직~~

로직이 헷갈렸지만 코드 구현은 그렇게 어렵지 않았었음!

import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int idx = 0;

		int[] answer = new int[commands.length];

		for (int i = 0; i < commands.length; i++) {
			int[] list = new int[commands[i][1] - commands[i][0] + 1];
			idx = 0;
			for (int j = commands[i][0] - 1; j < commands[i][1]; j++) {
				list[idx] = array[j];
				idx++;
			}
			Arrays.sort(list);
			answer[i] = list[commands[i][2] - 1];
		}
        return answer;
    }
}

 

728x90
반응형

댓글