-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaximum-bags-with-full-capacity-of-rocks.py
90 lines (80 loc) · 2.96 KB
/
maximum-bags-with-full-capacity-of-rocks.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 2279. Maximum Bags With Full Capacity of Rocks
# 🟠 Medium
#
# https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/
#
# Tags: Array - Greedy - Sorting
import timeit
from heapq import heapify, heappop
from typing import List
# This is a template that can be used as the starting point of a
# solution with minimal changes.
# We need to compute each bag's remaining space and then start adding
# rocks to the bags that have the least amount of remaining space,
# because we are trying to maximize the number of bags that we fill.
# One way to do that is to compute the remaining space and use a heap to
# store it, then start popping from the heap and subtracting the
# amount of rocks that we need to use to fill each bag from the amount
# of bags that we have remaining. Stop once we don't have enough rocks
# to fill the next bag or we have filled all the bags.
#
# Time complexity: O(n*log(n)) - We pop n elements from the heap at
# a log(n) cost.
# Space complexity: O(n) - The remaining capacity array.
#
# Runtime: 944 ms Beats 93.9%
# Memory: 22.2 MB Beats 49.34%
class UseHeap:
def maximumBags(
self, capacity: List[int], rocks: List[int], additionalRocks: int
) -> int:
remaining_space = [capacity[i] - rocks[i] for i in range(len(rocks))]
heapify(remaining_space)
remaining_rocks, maxed_out_bags = additionalRocks, 0
while (
remaining_space
and (current := heappop(remaining_space)) <= remaining_rocks
):
remaining_rocks -= current
maxed_out_bags += 1
return maxed_out_bags
# Time complexity: O(n*log(n)) - We sort the capacity array of n items.
# Space complexity: O(n) - The remaining capacity array.
#
# Runtime: 944 ms Beats 93.9%
# Memory: 22.4 MB Beats 32.89%
class UseSortedArray:
def maximumBags(
self, capacity: List[int], rocks: List[int], additionalRocks: int
) -> int:
rem, i = sorted([capacity[i] - rocks[i] for i in range(len(rocks))]), 0
while i < len(rocks) and rem[i] <= additionalRocks:
additionalRocks -= rem[i]
i += 1
return i
def test():
executors = [
UseHeap,
UseSortedArray,
]
tests = [
[[10, 2, 2], [2, 2, 0], 100, 3],
[[2, 3, 4, 5], [1, 2, 4, 4], 2, 3],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.maximumBags(t[0], t[1], t[2])
exp = t[3]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()