LeetCode solutions

2469. Longest Subsequence With Limited Sum

My accepted Python solution to LeetCode problem 2469, Longest Subsequence With Limited Sum, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.4MB, accepted 2026-01-01.

python
class Solution:
    def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        import bisect
        nums.sort()
        prefix = []
        total = 0
        for n in nums:
            total += n
            prefix.append(total)
        
        result = []
        for q in queries:
            idx = bisect.bisect_right(prefix, q)
            result.append(idx)
        return result

Source