题目
我的题解
先排序,从小往大买。使用自己的排序算法会超时,直接用Arrays.sort(costs);即可。
class Solution {
public int maxIceCream(int[] costs, int coins) {
int count = 0;
Arrays.sort(costs);
for (int ic : costs) {
if (ic <= coins) {
count++;
coins -= ic;
} else {
break;
}
}
return count;
}
}
总结
好简单的题,可能是为了让我建立自信才定为Medium吧
- 看了看题解,原来我这种方法叫贪心算法。
530

被折叠的 条评论
为什么被折叠?



