Skip to content

Commit

Permalink
Create 1498.Number-of-Subsequences-That-Satisfy-the-Given-Sum-Conditi…
Browse files Browse the repository at this point in the history
…on.cpp
  • Loading branch information
wisdompeak authored Sep 8, 2020
1 parent dced5b2 commit f9380f7
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int numSubseq(vector<int>& nums, int target)
{
sort(nums.begin(), nums.end());
int n = nums.size();
long M = 1e9+7;
long ret = 0;
vector<long>power(n+1);
power[0] = 1;
for (int i=1; i<=n; i++)
power[i] = power[i-1] * 2 % M;

int j = n-1;
for (int i=0; i<n; i++)
{
while (j>=0 && nums[i]+nums[j] > target)
j--;
if (j<i) break;
ret = (ret + power[j-i]) % M;
}
return ret;
}
};

0 comments on commit f9380f7

Please sign in to comment.