Skip to content

Commit b6c05a0

Browse files
authored
Merge pull request #865 from 0xff-dev/410
Add solution and test-cases for problem 410
2 parents 627fa8c + 6df5b1c commit b6c05a0

File tree

3 files changed

+66
-26
lines changed

3 files changed

+66
-26
lines changed

leetcode/401-500/0410.Split-Array-Largest-Sum/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [410.Split Array Largest Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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**.
5+
6+
Return the minimized largest sum of the split.
7+
8+
A **subarray** is a contiguous part of the array.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [7,2,5,10,8], k = 2
14+
Output: 18
15+
Explanation: There are four ways to split nums into two subarrays.
16+
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.
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Split Array Largest Sum
23-
```go
2421
```
25-
22+
Input: nums = [1,2,3,4,5], k = 2
23+
Output: 9
24+
Explanation: There are four ways to split nums into two subarrays.
25+
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.
26+
```
2627

2728
## 结语
2829

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
l := len(nums)
5+
sum := 0
6+
m := 0
7+
cache := make([][]int, l)
8+
for i := 0; i < l; i++ {
9+
sum += nums[i]
10+
m = max(m, nums[i])
11+
cache[i] = make([]int, k+1)
12+
for j := 0; j <= k; j++ {
13+
cache[i][j] = -1
14+
}
15+
cache[i][1] = sum
16+
if i+1 <= k {
17+
cache[i][i+1] = m
18+
}
19+
}
20+
var dfs func(int, int) int
21+
dfs = func(index, kk int) int {
22+
if index < 0 {
23+
return -1
24+
}
25+
if cache[index][kk] != -1 {
26+
return cache[index][kk]
27+
}
28+
cur := 0
29+
ans := -1
30+
for choose := index; choose >= kk-1; choose-- {
31+
cur += nums[choose]
32+
if r := dfs(choose-1, kk-1); r != -1 {
33+
x := max(cur, r)
34+
if ans == -1 || ans > x {
35+
ans = x
36+
}
37+
}
38+
}
39+
cache[index][kk] = ans
40+
return ans
41+
}
42+
return dfs(l-1, k)
543
}

leetcode/401-500/0410.Split-Array-Largest-Sum/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{7, 2, 5, 10, 8}, 2, 18},
18+
{"TestCase2", []int{1, 2, 3, 4, 5}, 2, 9},
19+
{"TestCase3", []int{2, 3, 1, 2, 4, 3}, 5, 4},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)