Skip to content

Commit

Permalink
Add solution for Capacity To Ship Packages Within D Days
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacknahil committed Mar 15, 2024
1 parent ce4792f commit e03f0c1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions camp1/week3/leetcode/capacity-to-ship-packages-within-d-days.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:

def check(val):
days_count=1
cur_sum=0
for weight in weights:
cur_sum+=weight
if cur_sum>val:
cur_sum=weight
days_count+=1
return days_count<=days

low=max(weights)
high=sum(weights)

while low<=high:
mid=low+(high-low)//2
if check(mid):
high=mid-1
else:
low=mid+1
return low

0 comments on commit e03f0c1

Please sign in to comment.