|
| 1 | +--- |
| 2 | +title: 410.分割数组的最大值 |
| 3 | +date: 2024-01-21 13:35:04 |
| 4 | +tags: [题解, LeetCode, 困难, 贪心, 数组, 二分查找, 二分, 动态规划, 前缀和] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】410.分割数组的最大值:二分 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/split-array-largest-sum/](https://leetcode.cn/problems/split-array-largest-sum/) |
| 10 | + |
| 11 | +<p>给定一个非负整数数组 <code>nums</code> 和一个整数 <code>m</code> ,你需要将这个数组分成 <code>m</code><em> </em>个非空的连续子数组。</p> |
| 12 | + |
| 13 | +<p>设计一个算法使得这 <code>m</code><em> </em>个子数组各自和的最大值最小。</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | + |
| 17 | +<p><strong>示例 1:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>输入:</strong>nums = [7,2,5,10,8], m = 2 |
| 21 | +<strong>输出:</strong>18 |
| 22 | +<strong>解释:</strong> |
| 23 | +一共有四种方法将 nums 分割为 2 个子数组。 |
| 24 | +其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。 |
| 25 | +因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。</pre> |
| 26 | + |
| 27 | +<p><strong>示例 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>输入:</strong>nums = [1,2,3,4,5], m = 2 |
| 31 | +<strong>输出:</strong>9 |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong>示例 3:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>输入:</strong>nums = [1,4,4], m = 3 |
| 38 | +<strong>输出:</strong>4 |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | + |
| 43 | +<p><strong>提示:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 47 | + <li><code>0 <= nums[i] <= 10<sup>6</sup></code></li> |
| 48 | + <li><code>1 <= m <= min(50, nums.length)</code></li> |
| 49 | +</ul> |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +## 方法一:二分 |
| 54 | + |
| 55 | +写一个函数```check(s)```,返回```能否```将数组```nums```划分为```k```部分且每一部分的最大值不超过```s```。 |
| 56 | + |
| 57 | +> 实现方法很简单,使用一个变量```cnt```来记录当前部分的元素和。 |
| 58 | +> |
| 59 | +> 遍历数组,如果当前元素大于```s```,则必不可能成功划分,直接返回```false```。 |
| 60 | +> |
| 61 | +> 若```cnt```加上当前元素超过了```s```,则将当前元素划分为一组(```k--```、```cnt = 0```)。 |
| 62 | +> |
| 63 | +> ```cnt```加上当前元素。 |
| 64 | +> |
| 65 | +> 遍历结束后,判断```k - 1```是否大于等于```0```。若是,则返回```true```,否则返回```false```。 |
| 66 | +
|
| 67 | +有了这样的函数,我们只需要在主函数中写一个二分,枚举值```mid```是否能通过```check```。 |
| 68 | + |
| 69 | +> ```l```初始值为```0```,```r```初始值为“无穷大”(数组中所有元素之和再加一)。 |
| 70 | +> |
| 71 | +> 当```l < r```时,令$mid = \lfloor \frac{l+r}{2} \rfloor$。 |
| 72 | +> |
| 73 | +> 如果```check(mid)```通过了,则说明```mid```为一种合法分法,尝试更小的值能否成功划分(令```r = mid```) |
| 74 | +> |
| 75 | +> 否则,说明```mid```太小了,无法划分,尝试更大的值能否成功划分(令```l = mid + 1```) |
| 76 | +
|
| 77 | +二分结束后,```l = r```,返回```l```即为答案。 |
| 78 | + |
| 79 | ++ 时间复杂度$O(len(nums)\times \log \sum nums)$ |
| 80 | ++ 空间复杂度$O(1)$ |
| 81 | + |
| 82 | +### AC代码 |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +private: |
| 89 | + bool check(vector<int>& nums, int k, int s) { |
| 90 | + int cnt = 0; |
| 91 | + for (int t : nums) { |
| 92 | + if (t > s) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + if (t + cnt > s) { |
| 96 | + k--; |
| 97 | + cnt = 0; |
| 98 | + } |
| 99 | + cnt += t; |
| 100 | + } |
| 101 | + return --k >= 0; |
| 102 | + } |
| 103 | +public: |
| 104 | + int splitArray(vector<int>& nums, int k) { |
| 105 | + int l = 0, r = accumulate(nums.begin(), nums.end(), 0) + 1; |
| 106 | + while (l < r) { |
| 107 | + int mid = (l + r) >> 1; |
| 108 | + if (check(nums, k, mid)) { |
| 109 | + r = mid; |
| 110 | + } |
| 111 | + else { |
| 112 | + l = mid + 1; |
| 113 | + } |
| 114 | + } |
| 115 | + return l; |
| 116 | + } |
| 117 | +}; |
| 118 | +``` |
| 119 | +
|
| 120 | +#### Python |
| 121 | +
|
| 122 | +```python |
| 123 | +# from typing import List |
| 124 | +
|
| 125 | +class Solution: |
| 126 | + def check(self, k: int, s: int) -> bool: |
| 127 | + cnt = 0 |
| 128 | + for t in self.nums: |
| 129 | + if t > s: |
| 130 | + return False |
| 131 | + if cnt + t > s: |
| 132 | + k -= 1 |
| 133 | + cnt = 0 |
| 134 | + cnt += t |
| 135 | + return k - 1 >= 0 |
| 136 | +
|
| 137 | + def splitArray(self, nums: List[int], k: int) -> int: |
| 138 | + self.nums = nums |
| 139 | + l, r = 0, sum(nums) + 1 |
| 140 | + while l < r: |
| 141 | + mid = (l + r) >> 1 |
| 142 | + if self.check(k, mid): |
| 143 | + r = mid |
| 144 | + else: |
| 145 | + l = mid + 1 |
| 146 | + return l |
| 147 | +``` |
| 148 | + |
| 149 | +> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/21/LeetCode%200410.%E5%88%86%E5%89%B2%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/)哦~ |
| 150 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/135728821](https://letmefly.blog.csdn.net/article/details/135728821) |
0 commit comments