文章目录
- Binary Search
- submit code class Solution: def shipWithinDays(self, weights: List[int], days: int) -> int: # the sum day under the capacity of content def sum_days(weights,content): sum_day = 0 sum_weight = 0 i = 0 while i < len(weights): sum_weight += weights[i] if(sum_weight == content): sum_day += 1 sum_weight = 0 elif(sum_weight <= content and i == len(weights) - 1): sum_day += 1 elif(sum_weight > content and sum_day <= len(weights)): sum_day += 1 sum_weight = 0 continue elif(weights[i] > content): return 0 i += 1 return sum_day left = max(weights) right = sum(weights) while left < right: mid = left + int((right - left) / 2) temp_sum = sum_days(weights, mid) if temp_sum <= days: right = mid else: left = mid + 1 return right full code class Solution: def shipWithinDays(self, weights, days): # 在容量为content的情况下运载完需要的总天数 def sum_days(weights,content): sum_day = 0 sum_weight = 0 i = 0 # print("len_weights:" + str(len(weights))) while i < len(weights): sum_weight += weights[i] # print("i: " + str(i) + "\tsum_day: " + str(sum_day) + "\tsum_weight:\t" + str(sum_weight)) if(sum_weight == content): sum_day += 1 sum_weight = 0 elif(sum_weight <= content and i == len(weights) - 1): sum_day += 1 elif(sum_weight > content and sum_day <= len(weights)): sum_day += 1 sum_weight = 0 continue elif(weights[i] > content): return 0 i += 1 return sum_day left = max(weights) # # 最大值为给定的天数和最大的重量 # # 极端情况下的最大值 # right = max(weights) * days # left = 1 # right = max(weights) * len(weights) right = sum(weights) print("initial right:\t" + str(len(weights))) #求取的是最小容量 while left < right: mid = left + int((right - left) / 2) print("left:\t" + str(left) + "\tright:\t" + str(right)) print("mid:\t" + str(mid)) temp_sum = sum_days(weights, mid) print("temp_sum:\t" + str(temp_sum)) print("------------------------") if temp_sum <= days: right = mid else: left = mid + 1 return right S = Solution() weights = [1,2,3,4,5,6,7,8,9,10] days = 5 # 15 # weights = [3,2,2,4,1,4] # days = 3 # 6 weights = [1,2,3,1,1] days = 4 # 3 weights = [1,2,3,4,5,6,7,8,9,10] days = 1 # 55 print(S.shipWithinDays(weights,days))
A conveyor belt has packages that must be shipped from one port to another within days days.
The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.
Binary Search
Binary Search
- submit code
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
# the sum day under the capacity of content
def sum_days(weights,content):
sum_day = 0
sum_weight = 0
i = 0
while i < len(weights):
sum_weight += weights[i]
if(sum_weight == content):
sum_day += 1
sum_weight = 0
elif(sum_weight <= content and i == len(weights) - 1):
sum_day += 1
elif(sum_weight > content and sum_day <= len(weights)):
sum_day += 1
sum_weight = 0
continue
elif(weights[i] > content):
return 0
i += 1
return sum_day
left = max(weights)
right = sum(weights)
while left < right:
mid = left + int((right - left) / 2)
temp_sum = sum_days(weights, mid)
if temp_sum <= days:
right = mid
else:
left = mid + 1
return right
- full code
class Solution:
def shipWithinDays(self, weights, days):
# 在容量为content的情况下运载完需要的总天数
def sum_days(weights,content):
sum_day = 0
sum_weight = 0
i = 0
# print("len_weights:" + str(len(weights)))
while i < len(weights):
sum_weight += weights[i]
# print("i: " + str(i) + "\tsum_day: " + str(sum_day) + "\tsum_weight:\t" + str(sum_weight))
if(sum_weight == content):
sum_day += 1
sum_weight = 0
elif(sum_weight <= content and i == len(weights) - 1):
sum_day += 1
elif(sum_weight > content and sum_day <= len(weights)):
sum_day += 1
sum_weight = 0
continue
elif(weights[i] > content):
return 0
i += 1
return sum_day
left = max(weights)
# # 最大值为给定的天数和最大的重量
# # 极端情况下的最大值
# right = max(weights) * days
# left = 1
# right = max(weights) * len(weights)
right = sum(weights)
print("initial right:\t" + str(len(weights)))
#求取的是最小容量
while left < right:
mid = left + int((right - left) / 2)
print("left:\t" + str(left) + "\tright:\t" + str(right))
print("mid:\t" + str(mid))
temp_sum = sum_days(weights, mid)
print("temp_sum:\t" + str(temp_sum))
print("------------------------")
if temp_sum <= days:
right = mid
else:
left = mid + 1
return right
S = Solution()
weights = [1,2,3,4,5,6,7,8,9,10]
days = 5
# 15
# weights = [3,2,2,4,1,4]
# days = 3
# 6
weights = [1,2,3,1,1]
days = 4
# 3
weights = [1,2,3,4,5,6,7,8,9,10]
days = 1
# 55
print(S.shipWithinDays(weights,days))
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
# the sum day under the capacity of content
def sum_days(weights,content):
sum_day = 0
sum_weight = 0
i = 0
while i < len(weights):
sum_weight += weights[i]
if(sum_weight == content):
sum_day += 1
sum_weight = 0
elif(sum_weight <= content and i == len(weights) - 1):
sum_day += 1
elif(sum_weight > content and sum_day <= len(weights)):
sum_day += 1
sum_weight = 0
continue
elif(weights[i] > content):
return 0
i += 1
return sum_day
left = max(weights)
right = sum(weights)
while left < right:
mid = left + int((right - left) / 2)
temp_sum = sum_days(weights, mid)
if temp_sum <= days:
right = mid
else:
left = mid + 1
return right
class Solution:
def shipWithinDays(self, weights, days):
# 在容量为content的情况下运载完需要的总天数
def sum_days(weights,content):
sum_day = 0
sum_weight = 0
i = 0
# print("len_weights:" + str(len(weights)))
while i < len(weights):
sum_weight += weights[i]
# print("i: " + str(i) + "\tsum_day: " + str(sum_day) + "\tsum_weight:\t" + str(sum_weight))
if(sum_weight == content):
sum_day += 1
sum_weight = 0
elif(sum_weight <= content and i == len(weights) - 1):
sum_day += 1
elif(sum_weight > content and sum_day <= len(weights)):
sum_day += 1
sum_weight = 0
continue
elif(weights[i] > content):
return 0
i += 1
return sum_day
left = max(weights)
# # 最大值为给定的天数和最大的重量
# # 极端情况下的最大值
# right = max(weights) * days
# left = 1
# right = max(weights) * len(weights)
right = sum(weights)
print("initial right:\t" + str(len(weights)))
#求取的是最小容量
while left < right:
mid = left + int((right - left) / 2)
print("left:\t" + str(left) + "\tright:\t" + str(right))
print("mid:\t" + str(mid))
temp_sum = sum_days(weights, mid)
print("temp_sum:\t" + str(temp_sum))
print("------------------------")
if temp_sum <= days:
right = mid
else:
left = mid + 1
return right
S = Solution()
weights = [1,2,3,4,5,6,7,8,9,10]
days = 5
# 15
# weights = [3,2,2,4,1,4]
# days = 3
# 6
weights = [1,2,3,1,1]
days = 4
# 3
weights = [1,2,3,4,5,6,7,8,9,10]
days = 1
# 55
print(S.shipWithinDays(weights,days))
