1833.雪糕的最大数量
链接:1833.雪糕的最大数量
难度:Medium
标签:贪心、数组、排序
简介:给你价格数组 costs 和现金量 coins ,请你计算并返回 Tony 用 coins 现金能够买到的雪糕的 最大数量 。
题解 1 - typescript
- 编辑时间:2021-07-02
- 执行用时:280ms
- 内存消耗:52.6MB
- 编程语言:typescript
- 解法介绍:每次取最小 cost。
function maxIceCream(costs: number[], coins: number): number {
  costs.sort((a, b) => a - b);
  let ans = 0;
  for (const cost of costs) {
    if (coins >= cost) {
      ans++;
      coins -= cost;
    } else break;
  }
  return ans;
}