23 lines
564 B
Python
23 lines
564 B
Python
def pieces_needed(nums: list[int], limit: int) -> int:
|
|
pieces = 1
|
|
total = limit
|
|
for num in nums:
|
|
if total - num >= 0:
|
|
total -= num
|
|
else:
|
|
total = limit - num
|
|
pieces += 1
|
|
return pieces
|
|
|
|
|
|
class Solution:
|
|
def splitArray(self, nums: list[int], k: int) -> int:
|
|
lo, hi = max(nums), sum(nums)
|
|
while lo < hi:
|
|
limit = (lo + hi) // 2
|
|
if pieces_needed(nums, limit) > k:
|
|
lo = limit + 1
|
|
else:
|
|
hi = limit
|
|
return lo
|