forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaysToSplitArrayIntoThreeSubarrays.cpp
118 lines (110 loc) · 3.66 KB
/
WaysToSplitArrayIntoThreeSubarrays.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Source : https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/
// Author : Hao Chen
// Date : 2021-05-11
/*****************************************************************************************************
*
* A split of an integer array is good if:
*
* The array is split into three non-empty contiguous subarrays - named left, mid, right
* respectively from left to right.
* The sum of the elements in left is less than or equal to the sum of the elements in mid,
* and the sum of the elements in mid is less than or equal to the sum of the elements in right.
*
* Given nums, an array of non-negative integers, return the number of good ways to split nums. As the
* number may be too large, return it modulo 10^9 + 7.
*
* Example 1:
*
* Input: nums = [1,1,1]
* Output: 1
* Explanation: The only good way to split nums is [1] [1] [1].
*
* Example 2:
*
* Input: nums = [1,2,2,2,5,0]
* Output: 3
* Explanation: There are three good ways of splitting nums:
* [1] [2] [2,2,5,0]
* [1] [2,2] [2,5,0]
* [1,2] [2,2] [5,0]
*
* Example 3:
*
* Input: nums = [3,2,1]
* Output: 0
* Explanation: There is no good way to split nums.
*
* Constraints:
*
* 3 <= nums.length <= 10^5
* 0 <= nums[i] <= 10^4
******************************************************************************************************/
const int MOD = (int) (1e9 + 7);
class Solution {
public:
int waysToSplit(vector<int>& nums) {
int len = nums.size();
vector<int> presum(len, 0);
presum[0] = nums[0];
for(int i=1; i<nums.size(); i++){
presum[i] = presum[i-1] + nums[i];
}
return waysToSplit_BS(presum); // Binary Search
//return waysToSplit_TLE(presum); // Time Limit Error
}
int binary_search(vector<int>& presum, int left, int i, bool searchLeft ) {
int len = presum.size();
int l = i, r = len-1;
int res = -1;
while(l <= r) {
int m = l + (r - l) / 2;
// if search Left, let middle item belong to left
// if search Right, let middle item belong to right
int x = searchLeft? 0 : 1;
int right = presum[len-1] - presum[m-x];
int mid = presum[m-x] - presum[i-1];
if (left <= mid && mid <= right) {
res = m;
if (searchLeft) r = m - 1;
else l = m + 1;
}else if (left > mid) {
l = m + 1;
}else {
r = m -1;
}
}
return res;
}
int waysToSplit_BS(vector<int>& presum) {
int len = presum.size();
long cnt = 0;
for(int i=0; i<len-2; i++){
if (presum[i] > (presum[len-1] - presum[i]) / 2) break;
//find the most right position
long l = binary_search(presum, presum[i], i+1, true);
//find the most right position
long r = binary_search(presum, presum[i], i+1, false);
if (l == -1 || r == -1 ) continue;
cnt += (r-l);
//cout << i << " - [" << l << "," << r << "]" << endl;
}
//cout << endl;
return cnt % MOD;
}
int waysToSplit_TLE(vector<int>& presum) {
int len = presum.size();
int cnt = 0;
int left, mid, right;
for(int i=0; i<len-2; i++){
left = presum[i];
for (int j=i+1; j<len-1; j++) {
mid = presum[j] - presum[i];
right = presum[len-1] - presum[j];
if (left <= mid && mid <= right) {
cnt++;
}
}
}
return cnt;
}
};