https://leetcode.cn/problems/apple-redistribution-into-boxes/description/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class leetCode {
public static void main(String[] args) {
int[] ints = {1,3,2};
int [] insta = {4,3,1,5,2};
minimumBoxes(ints,insta);
}
static int minimumBoxes(int[] apple, int[] capacity) {
Arrays.sort(capacity);
ArrayList<Integer> capacityList = new ArrayList<>();
int appleSum =0;
int max = capacity[capacity.length-1];
capacityList.add(max);
int right=0;
for (int i=0;i<apple.length;i++){
appleSum+=apple[i];
}
if (max>=apple[0]){
return 1;
}
for (int i=capacity.length-1;i>0;i--){
right = capacity[i-1];
max=max+right;
if (max<appleSum){
//箱子不够继续加
capacityList.add(max);
}else if (max+right>=appleSum){
//够了直接走人
//箱子够了直接输出
capacityList.add(max);
return capacityList.size();
}
}
return capacityList.size();
}
}