[栈] leetcode 503 Next Greater Element II

problem:https://leetcode.com/problems/next-greater-element-ii/

一道比较简单的单调队列题目。不过由于题目要求是循环的,需要两个pass,第二个pass处理循环生效的next greater,同时需要把下标已经超出范围的队首数据及时pop出来。

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums)
    {
        deque<int> q;
        vector<int> res(nums.size(), -1);
        for (int i = 0; i < nums.size(); i++)
        {
            while (!q.empty() && nums[i] > nums[q.back()])
            {
                res[q.back()] = nums[i];
                q.pop_back();
            }
            q.push_back(i);
        }
        for (int i = 0; i < nums.size(); i++)
        {
            while (!q.empty() && nums[i] > nums[q.back()])
            {
                res[q.back()] = nums[i];
                q.pop_back();
            }
            while (!q.empty() && q.front() <= i)q.pop_front();
            if (q.empty())break;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11268300.html

时间: 2024-10-14 08:05:12

[栈] leetcode 503 Next Greater Element II的相关文章

Leetcode 503. Next Greater Element II JAVA语言

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

503. Next Greater Element II

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

503. Next Greater Element II 下一个更大元素

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

leetcode496 - Next Greater Element I - easy &amp;&amp; leetcode503 - Next Greater Element II - medium

496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number o

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint

[Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

[栈] leetcode 1019 Next Greater Node In Linked List

problem:https://leetcode.com/problems/next-greater-node-in-linked-list/ 维护递减的单调栈.这道题对象是链表,不像数组可以快速通过下标索引,所以比较方便的做法是在栈中同时记录数字和对应的下标,并且默认填0,如果找到了比它大的第一个数,再修改下标对应的数字. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne

Baozi Leetcode solution 229: Major Element II

Problem Statement Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Outp