import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Solution {
public int solution(int k, int m, int[] score) {
int answer = 0;
Arrays.sort(score);
ArrayList<Integer> arr1 = new ArrayList<Integer>();
int scoreidx = score.length - 1;
int boxcnt = score.length / m;
int boxsum = 0;
for (int j = 0; j < boxcnt; j++) {
for (int i = 0; i < m; i++) {
arr1.add(score[scoreidx]);
scoreidx--;
}
Collections.sort(arr1);
boxsum = arr1.get(0) * arr1.size();
answer = answer + boxsum;
arr1.clear();
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
int k = 4;
int m = 3;
int[] score = { 4, 1, 2, 2, 4, 4, 4, 4, 1, 2, 4, 2 };
System.out.println(s.solution(k, m, score));
}
}
알고리즘 설명 : 나올수 있는 사과박스의 수는 한정되어있기에 사과박스수만큼의 반복문을 설정, 이후 정렬된 사과박스를 List에 넣어 정렬, idx0가 최저값이기에 해당 원소를 이용하여 값을 구함.
다른사람풀이 :
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; i >= m; i -= m){
answer += score[i - m] * m;
}
return answer;
}
}
SELECT a1.ANIMAL_ID , a1.NAME
from ANIMAL_OUTS as a1
left join ANIMAL_INS as a2
on a1.ANIMAL_ID = a2.ANIMAL_ID
where a1.ANIMAL_ID is not null and a2.ANIMAL_ID IS NULL
order by 1
select a.FLAVOR
from
(SELECT f1.FLAVOR, f1.TOTAL_ORDER
from FIRST_HALF f1
left join ICECREAM_INFO f2
on f1.FLAVOR = f2.FLAVOR
where f1.TOTAL_ORDER>3000 AND f1.FLAVOR in('vanilla','peach','watermelon','mango','strawberry','melon','orange','pineapple')
order by 2 desc)a