Skip to content

Commit

Permalink
Same As - Allocate Minimum Number Of Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Jan 18, 2022
1 parent c17d37b commit d8da401
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 11_Binary-Search/03. Capacity To Ship Packages Within D Days.py
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/

class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
# First Solve => Allocate Minimum Number Of Pages - Binary Search

l = max(weights); r = sum(weights); ans = -1

if len(weights) < days: return -1

def isValid(weight, days, mid):
currSumWt = 0
countOfDays = 1
for wt in weights:
currSumWt += wt
if currSumWt > mid:
countOfDays += 1
currSumWt = wt
if countOfDays > days: return False
else: return True

while l <= r:
mid = (l+r) // 2
if isValid(weights, days, mid):
ans = mid
r = mid - 1
else:
l = mid + 1

return ans

0 comments on commit d8da401

Please sign in to comment.