Skip to content

Commit d52af71

Browse files
committed
Add solution #1498
1 parent 156c1e1 commit d52af71

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,330 LeetCode solutions in JavaScript
1+
# 1,331 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1145,6 +1145,7 @@
11451145
1494|[Parallel Courses II](./solutions/1494-parallel-courses-ii.js)|Hard|
11461146
1496|[Path Crossing](./solutions/1496-path-crossing.js)|Easy|
11471147
1497|[Check If Array Pairs Are Divisible by k](./solutions/1497-check-if-array-pairs-are-divisible-by-k.js)|Medium|
1148+
1498|[Number of Subsequences That Satisfy the Given Sum Condition](./solutions/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js)|Medium|
11481149
1502|[Can Make Arithmetic Progression From Sequence](./solutions/1502-can-make-arithmetic-progression-from-sequence.js)|Easy|
11491150
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11501151
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1498. Number of Subsequences That Satisfy the Given Sum Condition
3+
* https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of integers nums and an integer target.
7+
*
8+
* Return the number of non-empty subsequences of nums such that the sum of the minimum and
9+
* maximum element on it is less or equal to target. Since the answer may be too large,
10+
* return it modulo 109 + 7.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @param {number} target
16+
* @return {number}
17+
*/
18+
var numSubseq = function(nums, target) {
19+
const MOD = 1e9 + 7;
20+
const sortedNums = nums.sort((a, b) => a - b);
21+
const length = nums.length;
22+
let result = 0;
23+
const powers = new Array(length).fill(1);
24+
25+
for (let i = 1; i < length; i++) {
26+
powers[i] = (powers[i - 1] * 2) % MOD;
27+
}
28+
29+
let left = 0;
30+
let right = length - 1;
31+
32+
while (left <= right) {
33+
if (sortedNums[left] + sortedNums[right] <= target) {
34+
result = (result + powers[right - left]) % MOD;
35+
left++;
36+
} else {
37+
right--;
38+
}
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy