Leetcode Problem 2389. Longest Subsequence With Limited Sum

2389. Longest Subsequence With Limited Sum

Leetcode Solutions

Sort and Prefix Sum with Binary Search

  1. Sort the nums array in non-decreasing order.
  2. Construct a prefix sum array presum from the sorted nums.
  3. For each query in queries, perform a binary search on presum to find the maximum index where presum[index] is less than or equal to the query value.
  4. Store the found index in the result array answer.
  5. Return the answer array.
UML Thumbnail

Sort and Linear Scan

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...