개발일기

프로그래머스 K번째수 파이썬, 자바 본문

Algorithm

프로그래머스 K번째수 파이썬, 자바

한둥둥 2024. 8. 15. 13:25

이제부터 나는 이도류가 될 것이다. Why? Python에 대한 오픈소스가 너무 많고 개발이 더 편해보인다. 그래서 이도류를 택하기로 했다. 

 

 

k 번째수 자바 코드 

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int i=0;i<commands.length;i++){
            int start = commands[i][0];
            int end = commands[i][1];
            int targetNum = commands[i][2];
            int []clone = Arrays.copyOfRange(array, start-1, end);
            
            Arrays.sort(clone);
            answer[i] = clone[targetNum-1];
        }
        
        return answer;
    }
}

 

1. commands [i][0]에서 start를 가져온다.

2. commands [i][1]에서 end를 가져옴. 

 

Arrays.copyOfRange(array, start-1, end)를 통해서 배열을 복사해. 

Arrays.sort를 통해서 정렬해준다. 

 

와 이게 몇 줄이야? 이제 파이썬 코드를 보자. 

 

파이썬 코드 작성 

def solution(array, commands):
    answer = []
    for i in range(len(commands)) :
        arr = array[commands[i][0]-1:commands[i][1]]
        arr.sort();
        answer.append(arr[commands[i][2]-1]);
    return answer

 

이게? 파이썬? 좋다 너무 짧다. 함수를 쓸 필요도 없다. 그냥 좋다. 

로직은 자바랑 똑같다.

array를 통해서 commands[i][0]-1:command[i][1] 을 통해서 배열을 복사한 것을 arr 배열에 넣어주었다. 

그걸 이제 sort()를 사용하여 정렬시켜주었을 뿐이다.