Capacity To Ship Packages Within D Days LeetCode solution in Python

1
# Runtime: 1388 ms (Top 11.51%) | Memory: 17.1 MB (Top 35.27%)
2
class Solution:
3
def shipWithinDays(self, weights: List[int], days: int) -> int:
4
def calc(m): # function calculate no of days for given weight
5
c, s = 0, 0
6
for i in weights:
7
if i + s > m:
8
c += 1
9
s = 0
10
s += i
11
if s > 0:
12
c += 1
13
return c
14

15
left, right = max(weights), sum(weights)
16
while left <= right:
17
mid = (left + right) // 2
18
if calc(mid) > days:
19
left = mid + 1
20
else:
21
right = mid - 1
22
return left

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0