難易度
Medium
アプローチ
Brute-force
class Solution {
public int maxIceCream(int[] costs, int coins) {
Arrays.sort(costs);
int result = 0;
for (int c : costs) {
coins -= c;
if (coins < 0) {
break;
}
result++;
}
return result;
}
/* WA
public int maxIceCream_WA(int[] costs, int coins) {
Arrays.sort(costs);
int count = 0;
int cost = 0;
for (int c : costs) {
if (cost < coins && c < coins) {
cost += c;
count++;
}
}
return count;
}
*/
}
- こちらの解説を参照しました