- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/135808
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/135808. 과일 장수
풀이 시간 : 24분
import java.util.*;
class Solution {
public int solution(int k, int m, int[] score) {
int answer = 0;
Arrays.sort(score);
for(int i=0; i<score.length; i++){
int minScore = score[i];
answer += minScore*m;
}
return answer;
}
}
//before
Arrays.sort(score);
for(int i=0; i<score.length; i++){
int minScore = score[i];
answer += minScore*m;
}
//after
Integer[] scoreBox = Arrays.stream(score).boxed().toArray(Integer[]::new);
Arrays.sort(scoreBox, Collections.reverseOrder());
for(int i=0; i<scoreBox.length; i++){
if((i+1) % m == 0){
int minScore = scoreBox[i];
answer += minScore*m;
}
}
풀이 시간 : 38분(첫 풀이 시간 포함)
import java.util.*;
class Solution {
public int solution(int k, int m, int[] score) {
int answer = 0;
//Arrays.sort(score);
Integer[] scoreBox = Arrays.stream(score).boxed().toArray(Integer[]::new);
Arrays.sort(scoreBox, Collections.reverseOrder());
for(int i=0; i<scoreBox.length; i++){
if((i+1) % m == 0){
int minScore = scoreBox[i];
answer += minScore*m;
}
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int k, int m, int[] score) {
int answer = 0;
Arrays.sort(score);
for(int i = score.length - m; i >= 0; i -= m)
answer += score[i] * m;
return answer;
}
}