2389. Longest Subsequence With Limited Sum
On This Page
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
nums.sort()
m = len(queries)
res = [0] * m
idx = sorted(range(m), key=lambda i: queries[i])
s = j = 0
for i in idx:
while j < len(nums) and s + nums[j] <= queries[i]:
s += nums[j]
j += 1
res[i] = j
return res