forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-subarrays-with-more-ones-than-zeros.cpp
53 lines (49 loc) · 1.6 KB
/
count-subarrays-with-more-ones-than-zeros.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
// Time: O(n)
// Space: O(n)
class Solution {
public:
int subarraysWithMoreZerosThanOnes(vector<int>& nums) {
static const int MOD = 1e9 + 7;
vector<int> lookup(2 * size(nums) + 1);
lookup[0 + size(nums)] = 1;
int result = 0, total = 0, same = 0, more = 0;
for (const auto& num : nums) {
total += (num == 1) ? 1 : -1;
const int new_same = lookup[total + size(nums)];
const int new_more = (num == 1) ? (same + more + 1) % MOD : (more - new_same + MOD) % MOD;
++lookup[total + size(nums)];
result = (result + new_more) % MOD;
same = new_same, more = new_more;
}
return result;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
int subarraysWithMoreZerosThanOnes(vector<int>& nums) {
static const int MOD = 1e9 + 7;
vector<int> lookup(2 * size(nums) + 1, -2);
lookup[0 + size(nums)] = -1;
vector<int> dp(size(nums));
int result = 0, total = 0;
for (int i = 0; i < size(nums); ++i) {
total += (nums[i] == 1) ? 1 : -1;
if (lookup[total + size(nums)] == -2) {
if (total > 0) {
dp[i] = i + 1;
}
} else {
const int j = lookup[total + size(nums)];
dp[i] = (j == -1) ? 0 : dp[j];
if (nums[i] > 0) {
dp[i] += (i - 1) - j;
}
}
lookup[total + size(nums)] = i;
result = (result + dp[i]) % MOD;
}
return result;
}
};