forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplit Array Largest Sum.cpp
66 lines (51 loc) · 1.53 KB
/
Split Array Largest Sum.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
/****************************************************************************
Leetcode - 410. Split Array Largest Sum (Hard)
Question:
Given an array nums 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.
Example 1:
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:
//piece refers to an individual subarray
int pieces(vector<int>nums,int max_val)
{
int sum = 0, piece = 1;
for(auto i:nums)
{
sum+=i;
if(sum>max_val)
{
piece++;
sum = i;
}
}
return piece;
}
int splitArray(vector<int>& nums, int m) {
int n = nums.size();
int l=INT_MIN, h=0;
for(auto i:nums)
{
l = max(l,i);
h+=i;
}
while(l<h)
{
int mid = l+(h-l)/2;
int piece = pieces(nums,mid);
if(piece > m)
l = mid+1;
else
h = mid;
}
return l;
}
};