Skip to content
Decoder

Maximum Ice Cream Bars

Medium

Problem

A shop sells n ice cream bars; bar i costs costs[i] coins. You have coins coins and may buy the bars in any order. Return the most bars you can buy.

Example 1

Input
costs = [1, 3, 2, 4, 1], coins = 7
Output
4
Why
1 + 1 + 2 + 3

Example 2

Input
costs = [10, 6, 8, 7, 7, 8], coins = 5
Output
0

Example 3

Input
costs = [1, 6, 3, 1, 2, 5], coins = 20
Output
6

Constraints

  • costs.length == n
  • 1 <= n <= 10^5
  • 1 <= costs[i] <= 10^5
  • 1 <= coins <= 10^8

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.