MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / splitArray

Method splitArray

python/0410-split-array-largest-sum.py:2–22  ·  view source on GitHub ↗
(self, nums: List[int], m: int)

Source from the content-addressed store, hash-verified

1class Solution:
2 def splitArray(self, nums: List[int], m: int) -> int:
3 def canSplit(largest):
4 subarray = 0
5 curSum = 0
6 for n in nums:
7 curSum += n
8 if curSum > largest:
9 subarray += 1
10 curSum = n
11 return subarray + 1 <= m
12
13 l, r = max(nums), sum(nums)
14 res = r
15 while l <= r:
16 mid = l + ((r - l) // 2)
17 if canSplit(mid):
18 res = mid
19 r = mid - 1
20 else:
21 l = mid + 1
22 return res

Callers

nothing calls this directly

Calls 3

canSplitFunction · 0.85
maxFunction · 0.50
sumFunction · 0.50

Tested by

no test coverage detected