diff --git a/solution/0400-0499/0410.Split Array Largest Sum/README.md b/solution/0400-0499/0410.Split Array Largest Sum/README.md index 4e3ab10fc7d7c..db6d937be49e4 100644 --- a/solution/0400-0499/0410.Split Array Largest Sum/README.md +++ b/solution/0400-0499/0410.Split Array Largest Sum/README.md @@ -45,11 +45,14 @@
  • 1 <= m <= min(50, nums.length)
  • - ## 解法 +二分查找。 + +二分枚举**子数组的和的最大值**,找到满足条件的最小值。 + ### **Python3** @@ -57,7 +60,26 @@ ```python - +class Solution: + def splitArray(self, nums: List[int], m: int) -> int: + def check(x): + s, cnt = 0, 1 + for num in nums: + if s + num > x: + cnt += 1 + s = num + else: + s += num + return cnt <= m + + left, right = max(nums), sum(nums) + while left < right: + mid = (left + right) >> 1 + if check(mid): + right = mid + else: + left = mid + 1 + return left ``` ### **Java** @@ -65,7 +87,108 @@ ```java +class Solution { + public int splitArray(int[] nums, int m) { + int mx = -1; + for (int num : nums) { + mx = Math.max(mx, num); + } + int left = mx, right = (int) 1e9; + while (left < right) { + int mid = (left + right) >> 1; + if (check(nums, m, mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } + + private boolean check(int[] nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { + ++cnt; + s = num; + } else { + s += num; + } + } + return cnt <= m; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int splitArray(vector& nums, int m) { + int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9; + while (left < right) { + int mid = left + right >> 1; + if (check(nums, m, mid)) right = mid; + else left = mid + 1; + } + return left; + } + + bool check(vector& nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { + ++cnt; + s = num; + } else { + s += num; + } + } + return cnt <= m; + } +}; +``` +### **Go** + +```go +func splitArray(nums []int, m int) int { + mx := -1 + for _, num := range nums { + mx = max(mx, num) + } + left, right := mx, int(1e9) + for left < right { + mid := (left + right) >> 1 + if check(nums, m, mid) { + right = mid + } else { + left = mid + 1 + } + } + return left +} + +func check(nums []int, m, x int) bool { + s, cnt := 0, 1 + for _, num := range nums { + if s+num > x { + cnt++ + s = num + } else { + s += num + } + } + return cnt <= m +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} ``` ### **...** diff --git a/solution/0400-0499/0410.Split Array Largest Sum/README_EN.md b/solution/0400-0499/0410.Split Array Largest Sum/README_EN.md index 68a431b517b28..445acb9025582 100644 --- a/solution/0400-0499/0410.Split Array Largest Sum/README_EN.md +++ b/solution/0400-0499/0410.Split Array Largest Sum/README_EN.md @@ -43,21 +43,142 @@ where the largest sum among the two subarrays is only 18.
  • 1 <= m <= min(50, nums.length)
  • - ## Solutions +Binary search. + ### **Python3** ```python - +class Solution: + def splitArray(self, nums: List[int], m: int) -> int: + def check(x): + s, cnt = 0, 1 + for num in nums: + if s + num > x: + cnt += 1 + s = num + else: + s += num + return cnt <= m + + left, right = max(nums), sum(nums) + while left < right: + mid = (left + right) >> 1 + if check(mid): + right = mid + else: + left = mid + 1 + return left ``` ### **Java** ```java +class Solution { + public int splitArray(int[] nums, int m) { + int mx = -1; + for (int num : nums) { + mx = Math.max(mx, num); + } + int left = mx, right = (int) 1e9; + while (left < right) { + int mid = (left + right) >> 1; + if (check(nums, m, mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } + + private boolean check(int[] nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { + ++cnt; + s = num; + } else { + s += num; + } + } + return cnt <= m; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int splitArray(vector& nums, int m) { + int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9; + while (left < right) { + int mid = left + right >> 1; + if (check(nums, m, mid)) right = mid; + else left = mid + 1; + } + return left; + } + + bool check(vector& nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { + ++cnt; + s = num; + } else { + s += num; + } + } + return cnt <= m; + } +}; +``` +### **Go** + +```go +func splitArray(nums []int, m int) int { + mx := -1 + for _, num := range nums { + mx = max(mx, num) + } + left, right := mx, int(1e9) + for left < right { + mid := (left + right) >> 1 + if check(nums, m, mid) { + right = mid + } else { + left = mid + 1 + } + } + return left +} + +func check(nums []int, m, x int) bool { + s, cnt := 0, 1 + for _, num := range nums { + if s+num > x { + cnt++ + s = num + } else { + s += num + } + } + return cnt <= m +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} ``` ### **...** diff --git a/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp b/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp new file mode 100644 index 0000000000000..81535a0fde7e2 --- /dev/null +++ b/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int splitArray(vector& nums, int m) { + int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9; + while (left < right) { + int mid = left + right >> 1; + if (check(nums, m, mid)) right = mid; + else left = mid + 1; + } + return left; + } + + bool check(vector& nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { + ++cnt; + s = num; + } else { + s += num; + } + } + return cnt <= m; + } +}; \ No newline at end of file diff --git a/solution/0400-0499/0410.Split Array Largest Sum/Solution.go b/solution/0400-0499/0410.Split Array Largest Sum/Solution.go new file mode 100644 index 0000000000000..0f7a002ecebb3 --- /dev/null +++ b/solution/0400-0499/0410.Split Array Largest Sum/Solution.go @@ -0,0 +1,36 @@ +func splitArray(nums []int, m int) int { + mx := -1 + for _, num := range nums { + mx = max(mx, num) + } + left, right := mx, int(1e9) + for left < right { + mid := (left + right) >> 1 + if check(nums, m, mid) { + right = mid + } else { + left = mid + 1 + } + } + return left +} + +func check(nums []int, m, x int) bool { + s, cnt := 0, 1 + for _, num := range nums { + if s+num > x { + cnt++ + s = num + } else { + s += num + } + } + return cnt <= m +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/0400-0499/0410.Split Array Largest Sum/Solution.java b/solution/0400-0499/0410.Split Array Largest Sum/Solution.java index 454ce74963fec..15bd0083f72f2 100644 --- a/solution/0400-0499/0410.Split Array Largest Sum/Solution.java +++ b/solution/0400-0499/0410.Split Array Largest Sum/Solution.java @@ -1,28 +1,31 @@ class Solution { public int splitArray(int[] nums, int m) { - long l = 0, r = 0; - for (int x : nums) { - l = Math.max(l, x); - r += x; + int mx = -1; + for (int num : nums) { + mx = Math.max(mx, num); } - while (l < r) { - long mid = l + r >>> 1; - if (check(nums, m, mid)) r = mid; - else l = mid + 1; + int left = mx, right = (int) 1e9; + while (left < right) { + int mid = (left + right) >> 1; + if (check(nums, m, mid)) { + right = mid; + } else { + left = mid + 1; + } } - return (int) r; + return left; } - private boolean check(int[] nums, int m, long cap) { - int cnt = 1; - long tot = 0; - for (int x : nums) { - tot += x; - if (tot > cap) { + private boolean check(int[] nums, int m, int x) { + int s = 0, cnt = 1; + for (int num : nums) { + if (s + num > x) { ++cnt; - tot = x; + s = num; + } else { + s += num; } } return cnt <= m; } -} +} \ No newline at end of file diff --git a/solution/0400-0499/0410.Split Array Largest Sum/Solution.py b/solution/0400-0499/0410.Split Array Largest Sum/Solution.py new file mode 100644 index 0000000000000..e0db3ecafadc1 --- /dev/null +++ b/solution/0400-0499/0410.Split Array Largest Sum/Solution.py @@ -0,0 +1,20 @@ +class Solution: + def splitArray(self, nums: List[int], m: int) -> int: + def check(x): + s, cnt = 0, 1 + for num in nums: + if s + num > x: + cnt += 1 + s = num + else: + s += num + return cnt <= m + + left, right = max(nums), sum(nums) + while left < right: + mid = (left + right) >> 1 + if check(mid): + right = mid + else: + left = mid + 1 + return left