0496.cpp (600B)
1 class Solution { 2 public: 3 vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2) { 4 vector<int> res(nums1.size(), -1); 5 unordered_map<int, int> um; 6 stack<int> st; 7 8 for (int i = 0; i < nums2.size(); i++) { 9 while (!st.empty() && nums2[i] > st.top()) { 10 um.insert(make_pair(st.top(), nums2[i])); 11 st.pop(); 12 } 13 st.push(nums2[i]); 14 } 15 16 for (int i = 0; i < nums1.size(); i++) 17 if (um.find(nums1[i]) != um.end()) res[i] = um[nums1[i]]; 18 19 return res; 20 } 21 };