[Leetcode] 1498. Number of Subsequences That Satisfy the Given Sum Condition
|2023. 5. 6. 13:16
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
answer = 0
nums.sort() # O(NlogN)
left, right = 0, len(nums) - 1
while left <= right: # O(N)
if nums[left] + nums[right] <= target:
if (left - right) in [0, 1]:
answer += 1
else:
answer += pow(2, right - left)
left += 1
else:
right -= 1
return answer % (10**9 + 7)
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 1572. Matrix Diagonal Sum (0) | 2023.05.09 |
---|---|
[Leetcode] 34. Find First and Last Position of Element in Sorted Array (0) | 2023.05.09 |
[Leetcode] 1456. Maximum Number of Vowels in a Substring of Given Length (0) | 2023.05.06 |
[Leetcode] 649. Dota2 Senate (0) | 2023.05.05 |
[Leetcode] 986. Interval List Intersections (0) | 2023.05.05 |