diff --git a/11_Binary-Search/03. Capacity To Ship Packages Within D Days.py b/11_Binary-Search/03. Capacity To Ship Packages Within D Days.py index b6c38401..f9182e0e 100644 --- a/11_Binary-Search/03. Capacity To Ship Packages Within D Days.py +++ b/11_Binary-Search/03. Capacity To Ship Packages Within D Days.py @@ -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 \ No newline at end of file