LeetCode 2389. and Finite Longest Subsequence

[LetMeFly] 2389. and limited longest subsequence

Leetcode topic link: https://leetcode.cn/problems/longest-subsequence-with-limited-sum/

You are given an n array of integers of length nums, and an array mof queries.

Returns an array mof where is the maximum length of subsequences whose sum of elements in is less than or equal to . answer answer[i] numsqueries[i]

A subsequence is an array obtained by deleting some elements (or not) from an array without changing the order of the remaining elements.

 

Example 1:

Input: nums = [4,5,2,1], queries = [3,10,21]
 Output: [2,3,4]
 Explanation: The answer corresponding to queries is as follows:
- The sum of the subsequence [2,1] is less than or equal to 3. It can be proved that the maximum length of a subsequence that meets the requirements of the topic is 2, so answer[0] = 2.
- The sum of the subsequence [4,5,1] is less than or equal to 10. It can be proved that the maximum length of a subsequence that meets the requirements of the topic is 3, so answer[1] = 3.
- The sum of the subsequence [4,5,2,1] is less than or equal to 21. It can be proved that the maximum length of a subsequence that meets the requirements of the topic is 4, so answer[2] = 4.

Example 2:

Input: nums = [2,3,4,5], queries = [1]
 Output: [0]
 Explanation: The empty subsequence is the only subsequence that satisfies the sum of elements less than or equal to 1, so answer[0] = 0.

 

hint:

  • n == nums.length
  • m == queries.length
  • 1 <= n, m <= 1000
  • 1 <= nums[i], queries[i] <= 106

Method 1: Prefix sum + dichotomy

This question is about "subsequence". What's the meaning? It means that the elements in the array are deleted at will, and there is no need to ensure continuity.

So if you want the sum of as many numbers as possible to not exceed q, of course you want these numbers to be as small as possible.

Therefore, we sort the elements in the original array from small to large, and then use the prefix sum method to make it nums[i]"the sum of elements from nums[0] to nums[i]".

In this way, when we are looking for the longest sequence of "sum not exceeding ", we only need to find the position of the last element that is not greater than qtwo points , which means that the sum of this element and all elements before it in the sorted nums array No more than , and this is the last position that satisfies the above conditions.numsqq

This location is what you want.

  • 时间复杂度 O ( l e n ( n u m s ) × log ⁡ l e n ( n u m s ) + l e n ( q u e r i e s ) × log ⁡ l e n ( n u m s ) ) O(len(nums)\times \log len(nums) + len(queries)\times\log len(nums)) O ( l e n ( n u m s )×loglen(nums)+len(queries)×logl e n ( n u m s )) , the time complexity of sorting isO ( len ( nums ) × log ⁡ len ( nums ) O(len(nums)\times \log len(nums)O ( l e n ( n u m s )×logl e n ( n u m s ) , the time complexity of each search isO ( log ⁡ len ( nums ) ) O(\log len(nums))O(loglen(nums))
  • Space complexity O ( log ⁡ len ( nums ) ) O(\log len(nums))O(logl e n ( n u m s )) , here we modifynums numsn u m s array andqueries queriesq u er i es array, but O ( log ⁡ len ( nums ) ) O(\log len(nums))is used for sortingO(logl e n ( n u m s )) space

AC code

C++

class Solution {
    
    
public:
    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {
    
    
        sort(nums.begin(), nums.end());
        for (int i = 1; i < nums.size(); i++) {
    
    
            nums[i] += nums[i - 1];
        }
        for (int& q : queries) {
    
    
            q = upper_bound(nums.begin(), nums.end(), q) - nums.begin();
        }
        return queries;
    }
};

Python

# from typing import List
# import bisect


class Solution:
    def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        nums.sort()
        for i in range(1, len(nums)):
            nums[i] += nums[i - 1]
        for i, q in enumerate(queries):  # 这里for q in queries的话,修改q是不会修改queries中的值的
            queries[i] = bisect.bisect_right(nums, q)
        return queries

Simultaneously publish the article on CSDN, it is not easy to be original, please attach the link to the original article ~
Tisfy: https://letmefly.blog.csdn.net/article/details/129618747

Guess you like

Origin blog.csdn.net/Tisfy/article/details/129618747