-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1833.py
41 lines (34 loc) · 969 Bytes
/
1833.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# [ LeetCode ] 1833. Maximum Ice Cream Bars
def solution(costs: list[int], coins: int) -> int:
answer: int = 0
costs.sort()
for cost in costs:
if cost > coins:
break
coins -= cost
answer += 1
return answer
def another_solution(costs: list[int], coins: int) -> int:
import heapq
answer: int = 0
heapq.heapify(costs)
while costs:
cost: int = heapq.heappop(costs)
if coins < cost:
break
coins -= cost
answer += 1
return answer
def count_sort_solution(costs: list[int], coins: int) -> int:
max_cost: int = max(costs)
bucket: list[int] = [0] * (max_cost + 1)
for cost in costs:
bucket[cost] += 1
answer: int = 0
for cost in range(1, max_cost+1):
if not bucket[cost]:
continue
count: int = min(bucket[cost], coins//cost)
answer += count
coins -= (cost * count)
return answer