본문 바로가기

카테고리 없음

프로그래머스_Level1_과일장수

풀이

import java.util.*;
class Solution {
    public int solution(int k, int m, int[] score) {
        int answer = 0;
        int boxSize = score.length/m;
        int startCount=score.length-1;
        Arrays.sort(score);
        for(int i=0; i<boxSize; i++){
                if(startCount>0){
                answer+=score[startCount-m+1]*m;  
                }
                startCount=startCount-m;
        }
        return answer;
    }
}

 

생각을 해보자.

사실 맨 처음에는 리스트를 사용해서 데이터들을 분류한뒤 분류된 데이터들에서 최소값들을 Math.min과 같은 내장함수를 이용하여 끄집어 낼 생각을 했지만 굳이 그렇게 하지않고도 Arrays.sort를 이용해서 int배열을 정렬시키고 정렬된 int배열에서 특정한 위치 즉 각 주머니의 최소값들만 구해주면 되는 더욱 간단한 문제이다.