diff --git a/leetcode/401-500/0410.Split-Array-Largest-Sum/README.md b/leetcode/401-500/0410.Split-Array-Largest-Sum/README.md index 0d436bab2..0819ac7c3 100644 --- a/leetcode/401-500/0410.Split-Array-Largest-Sum/README.md +++ b/leetcode/401-500/0410.Split-Array-Largest-Sum/README.md @@ -1,28 +1,29 @@ # [410.Split Array Largest Sum][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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**. + +Return the minimized largest sum of the split. + +A **subarray** is a contiguous part of the array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [7,2,5,10,8], k = 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. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Split Array Largest Sum -```go ``` - +Input: nums = [1,2,3,4,5], k = 2 +Output: 9 +Explanation: There are four ways to split nums into two subarrays. +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. +``` ## 结语 diff --git a/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution.go b/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution.go index d115ccf5e..88f709d82 100644 --- a/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution.go +++ b/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + l := len(nums) + sum := 0 + m := 0 + cache := make([][]int, l) + for i := 0; i < l; i++ { + sum += nums[i] + m = max(m, nums[i]) + cache[i] = make([]int, k+1) + for j := 0; j <= k; j++ { + cache[i][j] = -1 + } + cache[i][1] = sum + if i+1 <= k { + cache[i][i+1] = m + } + } + var dfs func(int, int) int + dfs = func(index, kk int) int { + if index < 0 { + return -1 + } + if cache[index][kk] != -1 { + return cache[index][kk] + } + cur := 0 + ans := -1 + for choose := index; choose >= kk-1; choose-- { + cur += nums[choose] + if r := dfs(choose-1, kk-1); r != -1 { + x := max(cur, r) + if ans == -1 || ans > x { + ans = x + } + } + } + cache[index][kk] = ans + return ans + } + return dfs(l-1, k) } diff --git a/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution_test.go b/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution_test.go index 14ff50eb4..a53a9039f 100644 --- a/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution_test.go +++ b/leetcode/401-500/0410.Split-Array-Largest-Sum/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{7, 2, 5, 10, 8}, 2, 18}, + {"TestCase2", []int{1, 2, 3, 4, 5}, 2, 9}, + {"TestCase3", []int{2, 3, 1, 2, 4, 3}, 5, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }