-
Notifications
You must be signed in to change notification settings - Fork 9
/
amz_n_of_trips.py
32 lines (23 loc) · 947 Bytes
/
amz_n_of_trips.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
#tripMaxWeight: 'int' representing the max weight per trip
#packagesWeight: array of 'int' representing the weight of each package
#TODO: dynamic programing
def deliver(tripMaxWeight, packagesWeight, ntrips):
if not packagesWeight:
return ntrips
number_of_packages = 0
current_weight = 0
#current_weight < tripMaxWeight is not enough
while packagesWeight and number_of_packages < 2 and current_weight < tripMaxWeight:
pkg = packagesWeight.pop()
"""if current_weight+pkg >= tripMaxWeight:
packagesWeight.append(pkg)
break"""
current_weight += pkg
number_of_packages+=1
return deliver(tripMaxWeight, packagesWeight, ntrips+1)
def minimumNumberOfTrips(tripMaxWeight, packagesWeight):
if tripMaxWeight == 0:
return 0
packagesWeight.sort(reverse=True)
val = deliver(tripMaxWeight, packagesWeight, 0)
return val