본문 바로가기

알고리즘

프로그래머스 - k번째수

https://programmers.co.kr/learn/courses/30/lessons/42748

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

Level1 문제이길래 간단할 줄 알았는데 생각보다 귀찮았던 문제였습니다.

제 풀이가 조금 복잡한건지 포스팅 후에는 다른 사람 풀이를 한번 참고해보도록 해야겠습니다.

이 문제에서는 디버깅이 중요하다고 느꼈고 실제로 차근차근히 풀면 금방 해결할 수 있었던 문제였던 것 같아요.4

알고리즘 문제 풀이를 꾸준히 하려고 노력 중이지만 여러 여건상 쉽지가 않네요 아쉽습니다 ㅎㅎ

정답 코드

class Solution {
        public int[] solution(int[] array, int[][] commands) {
            int[] answer = new int[commands.length];
            int cnt;
            for(int i=0; i<commands.length; i++){
                int[] temp = new int[commands[i][1]-commands[i][0]+1];
                cnt = 0;
                for(int j=commands[i][0]-1; j<=commands[i][1]-1; j++){
                    temp[cnt] = array[j];
                    cnt++;
                }
                Arrays.sort(temp);
                answer[i] = temp[commands[i][2]-1];
            }

            return answer;
        }
    }