leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1498.cpp (658B)


0 #define MOD (int)(1E9 + 7) 1 #define SIZE (int)(1E5 + 1) 2 int pows[SIZE] = {1}; 3 static const auto Initialize = [] { 4 ios_base::sync_with_stdio(false), cin.tie(nullptr); 5 for (int i = 1; i < SIZE; ++i) 6 pows[i] = (pows[i - 1] << 1) % MOD; 7 return nullptr; 8 }(); 9 10 class Solution { 11 public: 12 int numSubseq(vector<int> &nums, int target) { 13 sort(nums.begin(), nums.end()); 14 int n = nums.size(), l = 0, r = n - 1; 15 16 int res = 0; 17 while (l <= r) { 18 if (nums[l] + nums[r] > target) 19 r--; 20 else 21 res = (res + pows[r - l++]) % MOD; 22 } 23 return res; 24 } 25 };