Number of Subsequences That Satisfy the Given Sum Condition LeetCode solution in Python

1
# Runtime: 637 ms (Top 91.1%) | Memory: 29.39 MB (Top 30.6%)
2

3

4
class Solution:
5
def numSubseq(self, nums: List[int], target: int) -> int:
6
nums.sort()
7
left, right = 0, len(nums) - 1
8
count = 0
9
mod = 10**9 + 7
10

11
while left <= right:
12
if nums[left] + nums[right] > target:
13
right -= 1
14
else:
15
count += pow(2, right - left, mod)
16
left += 1
17

18
return count % mod

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0