-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0410.分割数组的最大值.java
90 lines (83 loc) · 1.81 KB
/
0410.分割数组的最大值.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* @lc app=leetcode.cn id=410 lang=java
*
* [410] 分割数组的最大值
*
* https://leetcode.cn/problems/split-array-largest-sum/description/
*
* algorithms
* Hard (59.05%)
* Likes: 902
* Dislikes: 0
* Total Accepted: 79.3K
* Total Submissions: 132.6K
* Testcase Example: '[7,2,5,10,8]\n2'
*
* 给定一个非负整数数组 nums 和一个整数 k ,你需要将这个数组分成 k 个非空的连续子数组。
*
* 设计一个算法使得这 k 个子数组各自和的最大值最小。
*
*
*
* 示例 1:
*
*
* 输入:nums = [7,2,5,10,8], k = 2
* 输出:18
* 解释:
* 一共有四种方法将 nums 分割为 2 个子数组。
* 其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。
* 因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。
*
* 示例 2:
*
*
* 输入:nums = [1,2,3,4,5], k = 2
* 输出:9
*
*
* 示例 3:
*
*
* 输入:nums = [1,4,4], k = 3
* 输出:4
*
*
*
*
* 提示:
*
*
* 1 <= nums.length <= 1000
* 0 <= nums[i] <= 10^6
* 1 <= k <= min(50, nums.length)
*
*
*/
// @lc code=start
class Solution {
public int splitArray(int[] nums, int k) {
int left = 0, right = 0;
for (int num : nums) {
left = Math.max(left, num);
right += num;
}
while (left < right) {
int mid = left + (right - left) / 2;
int count = 1, current = 0;
for (int num : nums) {
if (current + num > mid) {
count++;
current = 0;
}
current += num;
}
if (count <= k)
right = mid;
else
left = mid + 1;
}
return left;
}
}
// @lc code=end