Likou 1673. Find the most competitive subsequence (monotonically increasing stack)

1673. Find the most competitive subsequence

Give you an integer array nums and a positive integer k, and return the most competitive nums subsequence of length k.

The subsequence of an array is a sequence obtained by deleting some elements (possibly not deleting elements) from the array.

At the first position where subsequence a and subsequence b are not the same, if the number in a is less than the corresponding number in b, then we say that subsequence a is more competitive than subsequence b (under the same length). For example, [1,3,4] is more competitive than [1,3,5], and 4 is less than 5 in the first different position, which is the last position.

示例 1:

输入:nums = [3,5,2,6], k = 2
输出:[2,6]
解释:在所有可能的子序列集合 {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]} 中,[2,6] 最具竞争力。
示例 2:

输入:nums = [2,4,3,3,5,4,9,6], k = 4
输出:[2,3,3,4]
 

提示:

1 <= nums.length <= 105
0 <= nums[i] <= 109
1 <= k <= nums.length

answer:

Reading the questions shows that we can obviously use the stack to solve such problems.

We create a stack and then start traversing the nums array. Since there are many cases of stacking, we can analyze the conditions of the stacking. In this way, if the stack is not released, it is the stacking.
Therefore, we analyze the stack conditions:

The first is that the stack is not empty , and the element outside the stack is smaller than the element on the top of the stack . Therefore, in this case, we should pop the old element from the stack and push the new element onto the stack. This is because we have to choose the "most competitive" sequence, so we should make every element in the stack as small as possible.

But it is not only these conditions , because it is possible that after the above update, the elements in the stack are as small as possible, but the stack may not be filled, that is, the length of the returned subsequence is not enough, so we also need to judge whether to participate in the update Can its length compose the required k ?

Note : As long as the remaining length is enough to satisfy k, some people may think, don't we want to maintain a monotonically increasing stack, how can we ensure that we maintain a monotonically increasing stack?
At this time, you need to be aware that we don't actually want to completely maintain a monotonically increasing stack . We just try to maintain a monotonously increasing stack as much as possible , because paying attention can give full play to the value of each element. But if the maintenance fails, we actually "try our best ".
And some people will think that in this case, because we have no maintenance later, it may not be the most competitive sequence. But you have to find that before the following "disordered" sequence, we try to maintain it as much as possible, and the competitiveness starts from the head , so as long as the head is "competitive" enough, we don't care. The following sub-columns are listed .

Code:

int *mostCompetitive(int *nums, int numsSize, int k, int *returnSize)
{
    
    
    int *stack = (int*)malloc(sizeof(int) *numsSize);
    int top = 0;

    for (int i = 0; i < numsSize; i++) 
    {
    
    
        /* 当前栈非空 当前遍历到的数字小于栈顶的数字 并且当前遍历到的数字之后的数字个数满足k个序列的长度 */
        while (top > 0 && stack[top - 1] > nums[i] && numsSize - i + top > k) 
        {
    
    
            top--;
        }
        /* 入栈 */
        stack[top++] = nums[i];
    }
    
    *returnSize = k;
    return stack;
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/115251067