forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain4.cpp
54 lines (41 loc) · 1.21 KB
/
main4.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
/// Source : https://leetcode.com/problems/binary-subarrays-with-sum/
/// Author : liuyubobobo
/// Time : 2018-10-29
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Three Pointers
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSubarraysWithSum(vector<int>& A, int S) {
int i_low = 0, i_high = 0;
int sum_low = 0, sum_high = 0;
int res = 0;
for(int j = 0; j < A.size(); j ++){
sum_low += A[j];
while(sum_low > S)
sum_low -= A[i_low ++];
sum_high += A[j];
while((sum_high > S || (sum_high == S && A[i_high] == 0)) && i_high < j)
sum_high -= A[i_high ++];
if(sum_low == S)
res += (i_high - i_low + 1);
}
return res;
}
};
int main() {
vector<int> A1 = {1,0,1,0,1};
cout << Solution().numSubarraysWithSum(A1, 2) << endl;
// 4
vector<int> A2 = {0,0,0,0,0};
cout << Solution().numSubarraysWithSum(A2, 0) << endl;
// 15
vector<int> A3 = {0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0};
cout << Solution().numSubarraysWithSum(A3, 3) << endl;
// 48
return 0;
}