-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path410.cpp
More file actions
78 lines (75 loc) · 1.99 KB
/
410.cpp
File metadata and controls
78 lines (75 loc) · 1.99 KB
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
// class Solution {
// public:
// vector<vector<int>> dp;
// int solve(vector<int>& nums, int idx, int n, int m){
// if(m==1){
// int curr = 0;
// if(idx>=n)
// return 0;
// for(int i=idx;i<n; i++)
// curr += nums[i];
// return dp[idx][m] = curr;
// }
// if(idx>=n)
// return INT_MAX;
// if(dp[idx][m]!=-1)
// return dp[idx][m];
// int curr = 0; int res = INT_MAX;
// for(int i=idx; i<n; i++){
// curr += nums[i];
// int tmp = max(curr,solve(nums,i+1,n,m-1));
// if(tmp<res)
// res = tmp;
// }
// return dp[idx][m] = res;
// }
// int splitArray(vector<int>& nums, int m) {
// int n = nums.size();
// dp.resize(n+1,vector<int>(m+1,-1));
// return solve(nums,0,n,m);
// }
// };
class Solution {
public:
long long sum(vector<int>& A) {
long long s = 0;
for(int i = 0; i < A.size(); i++) {
s += A[i];
}
return s;
}
bool isPossible(vector<int>& A, long long s, int m) {
long long sums = 0;
for(int i = 0; i < A.size(); i++) {
if(A[i] > s) {
return false;
}
if(sums + A[i] > s) {
sums = A[i];
m--;
if(m < 0) {
return false;
}
} else {
sums += A[i];
}
}
if(sums > 0) {
m--;
}
return m >= 0;
}
int splitArray(vector<int>& A, int m) {
long long low = 0, high = sum(A), ans = 0;
while(low <= high) {
long mid = low + (high - low)/2;
if(isPossible(A, mid, m)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
};