LeetCode 1498. [Python] Number of Subsequences That Satisfy the Given Sum Condition
原题链接
中等
作者:
徐辰潇
,
2020-07-11 08:22:30
,
所有人可见
,
阅读 759
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
#TC: O(n log n) n = len(nums)
#SC: O(1)
nums.sort()
print(nums)
left = 0
right = len(nums) - 1
res = 0
while(left < right):
Min = nums[left]
Max = nums[right]
Sum = Min + Max
if Sum <= target:
count = right - left
res = res + 2**count
left += 1
else:
right -= 1
#left == right
if nums[left]*2<=target:
res += 1
return res % (10**9+7)