class Solution {
public int maxIceCream(int[] costs, int coins) {
Arrays.sort(costs);
//直接先给雪糕价钱数组排个序
int count = 0;
//记录可以买雪糕数
for(int i = 0; i < costs.length; i++){
//贪心:从总的钱里从小到大一次减去每根雪糕钱数
//直到钱数小于单支雪糕数退出循环
int cost = costs[i];
if (coins >= cost){
coins -= cost;
count++;
} else{
break;
}
}
return count;
//返回可买雪糕数,这题中等???太离谱了
}
}
Leecode1833.雪糕的最大数量-----数组、贪心
最新推荐文章于 2024-07-22 16:27:42 发布