Skip to content

Commit 73b5219

Browse files
committed
+ problem 410
1 parent 0c82425 commit 73b5219

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 410. Split Array Largest Sum
2+
Given an integer array `nums` and an integer `k`, split `nums` into `k` non-empty subarrays such that the largest sum of any subarray is **minimized**.
3+
4+
Return *the minimized largest sum of the split*.
5+
6+
A **subarray** is a contiguous part of the array.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> nums = [7,2,5,10,8], k = 2
11+
<strong>Output:</strong> 18
12+
<strong>Explanation:</strong> There are four ways to split nums into two subarrays.
13+
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.
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> nums = [1,2,3,4,5], k = 2
19+
<strong>Output:</strong> 9
20+
<strong>Explanation:</strong> There are four ways to split nums into two subarrays.
21+
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
22+
</pre>
23+
24+
#### Constraints:
25+
* `1 <= nums.length <= 1000`
26+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
27+
* `1 <= k <= min(50, nums.length)`
28+
29+
## Solutions (Rust)
30+
31+
### 1. Solution
32+
```Rust
33+
impl Solution {
34+
pub fn split_array(nums: Vec<i32>, k: i32) -> i32 {
35+
let mut dp = vec![vec![0; k as usize + 1]; nums.len() + 1];
36+
37+
for i in 1..=nums.len() {
38+
dp[i][1] = dp[i - 1][1] + nums[i - 1];
39+
}
40+
41+
for i in 2..=nums.len() {
42+
for j in 2..=i.min(k as usize) {
43+
dp[i][j] = i32::MAX;
44+
for x in j - 1..i {
45+
dp[i][j] = dp[i][j].min(dp[x][j - 1].max(dp[i][1] - dp[x][1]));
46+
}
47+
}
48+
}
49+
50+
dp[nums.len()][k as usize]
51+
}
52+
}
53+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 410. 分割数组的最大值
2+
给定一个非负整数数组 `nums` 和一个整数 `m` ,你需要将这个数组分成 `m` 个非空的连续子数组。
3+
4+
设计一个算法使得这 `m` 个子数组各自和的最大值最小。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [7,2,5,10,8], k = 2
9+
<strong>输出:</strong> 18
10+
<strong>解释:</strong>
11+
一共有四种方法将 nums 分割为 2 个子数组。
12+
其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。
13+
因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> nums = [1,2,3,4,5], k = 2
19+
<strong>输出:</strong> 9
20+
</pre>
21+
22+
#### 示例 2:
23+
<pre>
24+
<strong>输入:</strong> nums = [1,4,4], m = 3
25+
<strong>输出:</strong> 4
26+
</pre>
27+
28+
#### 提示:
29+
* `1 <= nums.length <= 1000`
30+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
31+
* `1 <= k <= min(50, nums.length)`
32+
33+
## 题解 (Rust)
34+
35+
### 1. 题解
36+
```Rust
37+
impl Solution {
38+
pub fn split_array(nums: Vec<i32>, k: i32) -> i32 {
39+
let mut dp = vec![vec![0; k as usize + 1]; nums.len() + 1];
40+
41+
for i in 1..=nums.len() {
42+
dp[i][1] = dp[i - 1][1] + nums[i - 1];
43+
}
44+
45+
for i in 2..=nums.len() {
46+
for j in 2..=i.min(k as usize) {
47+
dp[i][j] = i32::MAX;
48+
for x in j - 1..i {
49+
dp[i][j] = dp[i][j].min(dp[x][j - 1].max(dp[i][1] - dp[x][1]));
50+
}
51+
}
52+
}
53+
54+
dp[nums.len()][k as usize]
55+
}
56+
}
57+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn split_array(nums: Vec<i32>, k: i32) -> i32 {
3+
let mut dp = vec![vec![0; k as usize + 1]; nums.len() + 1];
4+
5+
for i in 1..=nums.len() {
6+
dp[i][1] = dp[i - 1][1] + nums[i - 1];
7+
}
8+
9+
for i in 2..=nums.len() {
10+
for j in 2..=i.min(k as usize) {
11+
dp[i][j] = i32::MAX;
12+
for x in j - 1..i {
13+
dp[i][j] = dp[i][j].min(dp[x][j - 1].max(dp[i][1] - dp[x][1]));
14+
}
15+
}
16+
}
17+
18+
dp[nums.len()][k as usize]
19+
}
20+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
[404][404l] |[Sum of Left Leaves][404] |![py]
222222
[405][405l] |[Convert a Number to Hexadecimal][405] |![py]
223223
[409][409l] |[Longest Palindrome][409] |![rs]
224+
[410][410l] |[Split Array Largest Sum][410] |![rs]
224225
[412][412l] |[Fizz Buzz][412] |![py]
225226
[413][413l] |[Arithmetic Slices][413] |![rb]&nbsp;&nbsp;![rs]
226227
[414][414l] |[Third Maximum Number][414] |![rs]
@@ -1375,6 +1376,7 @@
13751376
[404]:Problemset/0404-Sum%20of%20Left%20Leaves/README.md#404-sum-of-left-leaves
13761377
[405]:Problemset/0405-Convert%20a%20Number%20to%20Hexadecimal/README.md#405-convert-a-number-to-hexadecimal
13771378
[409]:Problemset/0409-Longest%20Palindrome/README.md#409-longest-palindrome
1379+
[410]:Problemset/0410-Split%20Array%20Largest%20Sum/README.md#410-split-array-largest-sum
13781380
[412]:Problemset/0412-Fizz%20Buzz/README.md#412-fizz-buzz
13791381
[413]:Problemset/0413-Arithmetic%20Slices/README.md#413-arithmetic-slices
13801382
[414]:Problemset/0414-Third%20Maximum%20Number/README.md#414-third-maximum-number
@@ -2530,6 +2532,7 @@
25302532
[404l]:https://leetcode.com/problems/sum-of-left-leaves/
25312533
[405l]:https://leetcode.com/problems/convert-a-number-to-hexadecimal/
25322534
[409l]:https://leetcode.com/problems/longest-palindrome/
2535+
[410l]:https://leetcode.com/problems/split-array-largest-sum/
25332536
[412l]:https://leetcode.com/problems/fizz-buzz/
25342537
[413l]:https://leetcode.com/problems/arithmetic-slices/
25352538
[414l]:https://leetcode.com/problems/third-maximum-number/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
[404][404l] |[左叶子之和][404] |![py]
222222
[405][405l] |[数字转换为十六进制数][405] |![py]
223223
[409][409l] |[最长回文串][409] |![rs]
224+
[410][410l] |[分割数组的最大值][410] |![rs]
224225
[412][412l] |[Fizz Buzz][412] |![py]
225226
[413][413l] |[等差数列划分][413] |![rb]&nbsp;&nbsp;![rs]
226227
[414][414l] |[第三大的数][414] |![rs]
@@ -1375,6 +1376,7 @@
13751376
[404]:Problemset/0404-Sum%20of%20Left%20Leaves/README_CN.md#404-左叶子之和
13761377
[405]:Problemset/0405-Convert%20a%20Number%20to%20Hexadecimal/README_CN.md#405-数字转换为十六进制数
13771378
[409]:Problemset/0409-Longest%20Palindrome/README_CN.md#409-最长回文串
1379+
[410]:Problemset/0410-Split%20Array%20Largest%20Sum/README_CN.md#410-分割数组的最大值
13781380
[412]:Problemset/0412-Fizz%20Buzz/README_CN.md#412-fizz-buzz
13791381
[413]:Problemset/0413-Arithmetic%20Slices/README_CN.md#413-等差数列划分
13801382
[414]:Problemset/0414-Third%20Maximum%20Number/README_CN.md#414-第三大的数
@@ -2530,6 +2532,7 @@
25302532
[404l]:https://leetcode.cn/problems/sum-of-left-leaves/
25312533
[405l]:https://leetcode.cn/problems/convert-a-number-to-hexadecimal/
25322534
[409l]:https://leetcode.cn/problems/longest-palindrome/
2535+
[410l]:https://leetcode.cn/problems/split-array-largest-sum/
25332536
[412l]:https://leetcode.cn/problems/fizz-buzz/
25342537
[413l]:https://leetcode.cn/problems/arithmetic-slices/
25352538
[414l]:https://leetcode.cn/problems/third-maximum-number/

0 commit comments

Comments
 (0)