Skip to content
LC-2389 Easy LeetCode

2389. Longest Subsequence With Limited Sum

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 73% Topics: Array, Binary Search, Greedy, Sorting, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn + qlogn)
# Space: O(1)

import bisect


# greedy, sort, binary search
class Solution(object):
    def answerQueries(self, nums, queries):
        """
        :type nums: List[int]
        :type queries: List[int]
        :rtype: List[int]
        """
        nums.sort()
        for i in xrange(len(nums)-1):
            nums[i+1] += nums[i]
        return [bisect.bisect_right(nums, q) for q in queries]

Solution from kamyu104/LeetCode-Solutions · MIT