未验证 提交 48785396 编写于 作者: K Keqi Huang 提交者: GitHub

Create 0410._Split_Array_Largest_Sum.md

上级 8aba54d2
# 410. Split Array Largest Sum
**<font color=red>难度: Hard</font>**
## 刷题内容
> 原题连接
* https://leetcode.com/problems/split-array-largest-sum/
> 内容描述
```
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.
Note:
If n is the length of array, assume the following constraints are satisfied:
1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)
Examples:
Input:
nums = [7,2,5,10,8]
m = 2
Output:
18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
```
## 解题方案
> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
思路就是二分,跟1014题一摸一样
```python
class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
l, r = max(max(nums), sum(nums) // m), sum(nums)
while l < r:
mid = l + ((r-l) >> 1)
buckets, sums, works = 1, 0, True
for w in nums:
if w > mid:
works = False
break
if sums + w > mid:
buckets += 1
sums = 0
sums += w
if works and buckets <= m:
r = mid
else:
l = mid + 1
return l
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册