Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 789350b

Browse files
authored
Create 410-Split-Array-Largest-Sum.py
1 parent df227de commit 789350b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

410-Split-Array-Largest-Sum.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class 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

0 commit comments

Comments
 (0)