Skip to content

Commit 0571ffa

Browse files
authored
Create number-of-subsequences-that-satisfy-the-given-sum-condition.py
1 parent b86e85d commit 0571ffa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def numSubseq(self, nums, target):
6+
"""
7+
:type nums: List[int]
8+
:type target: int
9+
:rtype: int
10+
"""
11+
MOD = 10**9 + 7
12+
nums.sort()
13+
result = 0
14+
left, right = 0, len(nums)-1
15+
while left <= right:
16+
if nums[left]+nums[right] > target:
17+
right -= 1
18+
else:
19+
result = (result+pow(2, right-left, MOD))%MOD
20+
left += 1
21+
return result

0 commit comments

Comments
 (0)