Untitled
class Solution { public: vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) { int sz_num1=nums1.size(); int sz_num2=nums2.size(); vector<int>ans(sz_num1,-1); map<int,int>mp; for(int i=0;i<sz_num1;++i){ mp[nums1[i]]=i; } stack<int>st; for(int i=0;i<sz_num2;++i){ int cur=nums2[i]; while(!st.empty()&&cur>st.top()){ int val=st.top(); st.pop(); ans[mp[val]]=cur; } if(mp.find(cur)!=mp.end())st.push(cur); } return ans; } };
Leave a Comment