Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 736 Bytes

2023-01-06.md

File metadata and controls

24 lines (18 loc) · 736 Bytes

题目

  1. Maximum Ice Cream Bars

It is a sweltering summer day, and a boy wants to buy some ice cream bars.

At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible.

Return the maximum number of ice cream bars the boy can buy with coins coins.

Note: The boy can buy the ice cream bars in any order.

思路

代码

var maxIceCream = function(costs, coins) {
  costs.sort((a, b) => a - b)
  for (let i = 0; i < costs.length; i++) {
    coins -= costs[i]
    if (coins < 0) return i
  }
  return costs.length
}