[프로그래머스] Lv.0 뒤에서 5등 위로

이다혜·2023년 10월 19일
0

프로그래머스

목록 보기
10/61
post-thumbnail

📎 문제 출처

https://school.programmers.co.kr/learn/courses/30/lessons/181852

📌 문제 설명

정수로 이루어진 리스트 num_list가 주어집니다. num_list에서 가장 작은 5개의 수를 제외한 수들을 오름차순으로 담은 리스트를 return하도록 solution 함수를 완성해주세요.

❓ 풀이 방법

가장 작은 5개의 수를 제외하기 위해 주어진 배열을 Arrays.sort로 오름차순 정렬시킵니다.
그 후 앞의 5개를 제외한 요소들을 answer 배열에 저장합니다.

💻 Code

import java.util.*;

class Solution {
    public int[] solution(int[] num_list) {
        int len = num_list.length;
        int[] answer = new int[len-5];
        
        Arrays.sort(num_list);
        
        int index = 0;
        for(int i = 5; i < len; i++) {
            answer[index++] = num_list[i];
        }
        return answer;
    }
}

0개의 댓글