503. Next Greater Element II

时间:2023-03-09 15:15:57
503. Next Greater Element II

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

class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
stack<int> st;
int n = nums.size();
for (int i = * n - ; i >= n; i--) {
int cur = nums[i % n];
while (!st.empty() && cur >= st.top())
st.pop();
st.push(cur);
} vector<int> res(n, );
for (int i = n - ; i >= ; i--) {
int cur = nums[i];
while (!st.empty() && cur >= st.top())
st.pop();
if (st.empty())
res[i] = -;
else
res[i] = st.top();
st.push(cur);
} return res;
}
};