拿出最少数目的魔法豆 (Removing Minimum Number of Magic Beans)

 

思路:

// @Title: 拿出最少数目的魔法豆 (Removing Minimum Number of Magic Beans)
// @Author: qisiii
// @Date: 2024-01-18 10:27:40
// @Runtime: 37 ms
// @Memory: 62.9 MB
// @comment: 
// @flag: 
class Solution {
    public long minimumRemoval(int[] beans) {
        Arrays.sort(beans);
        //总数,可能达到上限,所以需要用long
        long s=0;
        for(int i:beans){
            s=s+i;
        }
        long r=s;
        //如果以第i位为基准,那么剩余总数是beans[i]*(beans.length-i)
        for(int i=0;i<beans.length;i++){
            r=Math.min(r,s-((long)beans[i]*(beans.length-i)));
        }
        return r;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18