Processing math: 100%

Leetcode 1833 - Maximum Ice Cream Bars

題目

Problem#

給你 n 個冰淇淋價格是 cost[i] 今天你有 coins 元,問你最多可以買幾個?

想法#

sort 從最小的開始挑

  • 時間複雜度: O(nlogn)
  • 空間複雜度: O(1)

AC Code#

Copy
class Solution {
public:
int maxIceCream(vector<int>& N, int M) {
int ans = 0;
sort(N.begin(), N.end());
for(int i : N)
{
if(M >= i)
{
M -= i;
ans++;
}
}
return ans;
}
};
view raw leetcode/1833.cpp delivered with ❤ by emgithub

心得#

水題 應該是 Easy