Skip to content

Commit e1571fb

Browse files
committed
feat(leetcode): 1498. Number of Subsequences That Satisfy the Given Sum Condition : 双指针
1 parent 5cc639f commit e1571fb

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var numSubseq = function(nums, target) {
7+
const n = nums.length, P = 1e9+7
8+
nums.sort((a, b) => a-b)
9+
let ans = 0
10+
for (let i = 0, j = n-1; i < n; i++) {
11+
while (j > i && nums[i] + nums[j] > target) j--
12+
if (j < i || nums[i] + nums[j] > target) break
13+
ans = (ans + Number(2n ** BigInt(j-i) % BigInt(P))) % P
14+
}
15+
return ans
16+
};

leetcode/残酷刷题/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- 424. Longest Repeating Character Replacement
7171
- 930. Binary Subarrays With Sum
7272
- 2106. Maximum Fruits Harvested After at Most K Steps
73+
- 1498. Number of Subsequences That Satisfy the Given Sum Condition
7374

7475
###
7576

0 commit comments

Comments
 (0)