2389. Longest Subsequence With Limited Sum

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2389

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