2389. Longest Subsequence With Limited Sum
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
prefix_sum = list(accumulate(sorted(nums)))
answer = []
for query in queries:
result = 0
for i, v in enumerate(prefix_sum):
if v <= query:
result = i+1
else:
break
answer.append(result)
return answer
O(m*n + n)
O(n)
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
prefix = list(accumulate(sorted(nums)))
return [bisect.bisect_right(prefix, i) for i in queries]
O(mlogn + n)
O(n)
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
| |
\---------/
\_______/