Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 738444a

Browse files
aQuaaQua
aQua
authored and
aQua
committed
410 added
1 parent b50a122 commit 738444a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [410. Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)
2+
3+
## 题目
4+
5+
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.
6+
7+
Note:
8+
If n is the length of array, assume the following constraints are satisfied:
9+
10+
1. 1 ≤ n ≤ 1000
11+
1. 1 ≤ m ≤ min(50, n)
12+
13+
Examples:
14+
15+
```text
16+
Input:
17+
nums = [7,2,5,10,8]
18+
m = 2
19+
20+
Output:
21+
18
22+
23+
Explanation:
24+
There are four ways to split nums into two subarrays.
25+
The best way is to split it into [7,2,5] and [10,8],
26+
where the largest sum among the two subarrays is only 18.
27+
```
28+
29+
## 解题思路
30+
31+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Problem0410
2+
3+
func splitArray(nums []int, m int) int {
4+
res :=
5+
6+
return res
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Problem0410
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
nums []int
13+
m int
14+
ans int
15+
}{
16+
17+
{
18+
[]int{7, 2, 5, 10, 8},
19+
2,
20+
18,
21+
},
22+
23+
// 可以有多个 testcase
24+
}
25+
26+
func Test_splitArray(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
for _, tc := range tcs {
30+
fmt.Printf("~~%v~~\n", tc)
31+
ast.Equal(tc.ans, splitArray(tc.nums, tc.m), "输入:%v", tc)
32+
}
33+
}
34+
35+
func Benchmark_splitArray(b *testing.B) {
36+
for i := 0; i < b.N; i++ {
37+
for _, tc := range tcs {
38+
splitArray(tc.nums, tc.m)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)