-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Solution: | ||
def shipWithinDays(self, weights: List[int], D: int) -> int: | ||
""" | ||
解法:二分搜索 | ||
target = D,也就是说,当天数恰好为D时,返回重量。 | ||
""" | ||
|
||
def count_days(weight): | ||
""" | ||
当重量为weight时,返回需要的天数 | ||
""" | ||
if weight < mi: | ||
return D + 1 | ||
count = 1 | ||
tem = weight | ||
for x in weights: | ||
if tem >= x: | ||
tem -= x | ||
else: | ||
count += 1 | ||
tem = weight - x | ||
return count | ||
|
||
def bin_search(low, high): | ||
if low > high: | ||
return low | ||
mid = (low + high) // 2 | ||
days = count_days(mid) | ||
print(mid, days) | ||
if days <= D: | ||
return bin_search(low, mid - 1) | ||
else: | ||
return bin_search(mid + 1, high) | ||
|
||
if len(weights) < D: | ||
return max(weights) | ||
mi = max(weights) | ||
ma = sum(weights) | ||
return bin_search(mi, ma) | ||
|