과일 장수 Java/ Kotlin

푸른하늘·2022년 11월 20일
0

문제 출처

Java

import java.util.*;
class Solution {
  public int solution(int k, int m, int[] score) {
        int answer = 0;
        Arrays.sort(score); // [1,1,1,2,2,3,3]
    
        int [] tScore = new int[score.length];

        for (int i= 0; i<score.length; i++){
            tScore[i] = score[score.length -1 -i];
            
        }
        int index = 0;
        while (true){
            if (index >= tScore.length || index + m > tScore.length) {  break;}
            answer += tScore[index +m-1] * m;
            index += m;
        }

        return answer;

    }
}

Kotlin

class Solution {
    fun solution(k: Int, m: Int, score: IntArray): Int {
        var answer: Int = 0
        val score = score.sorted().reversed()
        var index : Int = 0
        while (true){
            if(index >= score.size || index+m > score.size){
                break
            }
            answer += score[index+ m -1] * m
            index += m
        }
        return answer
    }
}
profile
Developer-Android-CK

0개의 댓글