-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmake-array-elements-equal-to-zero.cpp
42 lines (40 loc) · 1.26 KB
/
make-array-elements-equal-to-zero.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
// Time: O(n)
// Space: O(1)
// prefix sum, CodeChef Starters 146 - Bouncing Ball (https://www.codechef.com/problems/BOUNCE_BALL)
class Solution {
public:
int countValidSelections(vector<int>& nums) {
int result = 0;
for (int i = 0, curr = 0, total = accumulate(cbegin(nums), cend(nums), 0); i < size(nums); ++i) {
if (nums[i] == 0) {
result += max(2 - abs(curr - (total - curr)), 0);
} else {
curr += nums[i];
}
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// prefix sum, CodeChef Starters 146 - Bouncing Ball (https://www.codechef.com/problems/BOUNCE_BALL)
class Solution2 {
public:
int countValidSelections(vector<int>& nums) {
vector<int> prefix(size(nums) + 1);
for (int i = 0; i < size(nums); ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
vector<int> suffix(size(nums) + 1);
for (int i = size(nums) - 1; i >= 0; --i) {
suffix[i] = suffix[i + 1] + nums[i];
}
int result = 0;
for (int i = 0; i < size(nums); ++i) {
if (nums[i] == 0) {
result += max(2 - abs(prefix[i] - suffix[i + 1]), 0);
}
}
return result;
}
};