-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboats-to-save-people.py
68 lines (61 loc) · 2.17 KB
/
boats-to-save-people.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
# 881. Boats to Save People
# 🟠 Medium
#
# https://leetcode.com/problems/boats-to-save-people/
#
# Tags: Array - Two Pointers - Greedy - Sorting
import timeit
from typing import List
# Sort the weights of the people that we need to save, then use two
# pointers, one for the heaviest person that needs to be saved and one
# for the lightest, if we can put them together on a boat, do so and
# shift both pointers, otherwise put the heaviest person alone in a boat
# and move only that pointer. This works because if the heaviest person
# cannot go on the boat together with the current lightest person, we
# know that it cannot also go with any other.
#
# Time complexity: O(n*log(n)) - Sorting has the most complexity, then
# arranging people on boats we can do in O(n).
# Space complexity: O(n) - Sorting takes memory in Python.
#
# Runtime 453 ms Beats 84.42%
# Memory 20.9 MB Beats 60.46%
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l, r = 0, len(people) - 1
res = 0
while l <= r:
# Consume one boat.
res += 1
# If we can fit both people on this boat, do it and move to
# the next likely pair.
if people[l] + people[r] <= limit:
l += 1
# The heavy person always goes in the boat.
r -= 1
return res
def test():
executors = [Solution]
tests = [
[[1, 2], 3, 1],
[[3, 2, 2, 1], 3, 3],
[[3, 5, 3, 4], 5, 4],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.numRescueBoats(t[0], t[1])
exp = t[2]
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()