题解 | #奶牛的下一个元素#
奶牛的下一个元素
https://www.nowcoder.com/practice/01ce43ed6ff0470ca9b8c459f2745299
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> nextGreaterCowWeight(vector<int>& nums) { stack<int> st; int len = nums.size(); vector<int> ans(len, -1); for (int i = 0; i < 2 * len; ++i) { while (!st.empty() && (nums[i % len] > nums[st.top()])) { int index = st.top(); st.pop(); ans[index] = nums[i % len]; } st.push(i % len); } return ans; } };