Time Cost
8min0s
Implementation
The solution’s time complexity is n*log(n). Some boundaries should be noticed such as “hour_needed” could be over maximum integer.
Code
- My Solution
class Solution { public: vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) { vector<int> result(nums1.size(), -1); unordered_map<int, int> nextgreat; stack<int> st; int tmp = 0; for (int num: nums2) { while (!st.empty()) { tmp = st.top(); if (tmp < num) { nextgreat.emplace(tmp, num); st.pop(); }else { break; } } st.push(num); } for (int i=0; i<nums1.size(); i++) { if (nextgreat.count(nums1[i])) { result[i] = nextgreat[nums1[i]]; } } return result; } };