forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitArrayLargestSum.cpp
69 lines (64 loc) · 2.18 KB
/
SplitArrayLargestSum.cpp
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
// Source : https://leetcode.com/problems/split-array-largest-sum/
// Author : Hao Chen
// Date : 2016-11-13
/***************************************************************************************
*
* Given an array which consists of non-negative integers and an integer m, you can
* split the array into m non-empty continuous subarrays. Write an algorithm to
* minimize the largest sum among these m subarrays.
*
* Note:
* Given m satisfies the following constraint: 1 ≤ m ≤ length(nums) ≤ 14,000.
*
* Examples:
*
* Input:
* nums = [7,2,5,10,8]
* m = 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.
***************************************************************************************/
class Solution {
public:
// Idea
// 1) The max of the result is the sum of the whole array.
// 2) The min of the result is the max num among the array.
// 3) Then, we use Binary Search to find the resullt between the `min` and the `max`
int splitArray(vector<int>& nums, int m) {
int min = 0, max = 0;
for (int n : nums) {
min = std::max(min, n);
max += n;
}
while (min < max) {
int mid = min + (max - min) / 2;
if (hasSmallerSum(nums, m, mid)) max = mid;
else min = mid + 1;
}
return min;
}
// Using a specific `sum` to check wheter we can get `smaller sum`
// The idea here as below:
// find all of possible `sub array` whose sum greater than `sum`
// 1) if the number of `sub array` > m, whcih means the actual result is greater than `sum`
// 2) if the number of `sub array` <= m, whcih means we can have `smaller sum`
//
bool hasSmallerSum(vector<int>& nums, int m, int sum) {
int cnt = 1, curSum = 0;
for (int n : nums) {
curSum += n;
if (curSum > sum) {
curSum = n;
cnt++;
if (cnt > m) return false;
}
}
return true;
}
};