Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int atMost(vector<int> & A, int S){
- int ans, left, right;
- ans = 0;
- if(S < 0) return 0;
- for(left=right=0; right<A.size(); right++){
- S -= A[right];
- while(S < 0)
- S += A[left++];
- ans += right-left+1;
- }
- return ans;
- }
- int numSubarraysWithSum(vector<int>& A, int S) {
- return atMost(A, S) - atMost(A, S-1);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement