Skip to content

Commit 8ee802a

Browse files
feat(min-num-boats): add min number of boats challenge
1 parent f713321 commit 8ee802a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

min-num-boats/min-num-boats.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# weight the boat can carry 190kg
2+
# max 2 people per boat
3+
# list of peoples weights [20, 30, 40, 60, 60, 90]
4+
5+
"""
6+
90
7+
60 + 20
8+
60 + 30
9+
40
10+
11+
90
12+
60
13+
60 + 20
14+
40 + 30
15+
16+
90 + 20
17+
60 + 30
18+
60 + 40
19+
"""
20+
21+
22+
def min_num_boats(max_weight, people):
23+
if not people:
24+
return 0
25+
26+
if max_weight <= 0:
27+
raise Exception('invalid max weight')
28+
29+
min_num = 0
30+
left = 0
31+
right = len(people) - 1
32+
while left <= right:
33+
currentWeight = people[right]
34+
if currentWeight > max_weight:
35+
raise Exception('boat not heavy enough for person')
36+
37+
right -= 1
38+
if currentWeight + people[left] <= max_weight:
39+
left += 1
40+
min_num += 1
41+
42+
return min_num
43+
44+
45+
weight = 100
46+
people = [20, 30, 40, 60, 80, 90]
47+
print(min_num_boats(weight, people))

0 commit comments

Comments
 (0)