Skip to content

Commit

Permalink
Update and rename 496.Next Greater Element I.cpp to 496.Next-Greater-…
Browse files Browse the repository at this point in the history
…Element-I.cpp
  • Loading branch information
wisdompeak authored Dec 19, 2021
1 parent 39507fc commit 4523734
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
29 changes: 0 additions & 29 deletions Stack/496.Next-Greater-Element-I/496.Next Greater Element I.cpp

This file was deleted.

28 changes: 28 additions & 0 deletions Stack/496.Next-Greater-Element-I/496.Next-Greater-Element-I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
unordered_map<int,int>nextGreater;
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2)
{
int n = nums2.size();
stack<int>Stack; // index
for (int i = 0; i < n; i++)
{
while (!Stack.empty() && nums2[Stack.top()] < nums2[i])
{
nextGreater[nums2[Stack.top()]] = nums2[i];
Stack.pop();
}
Stack.push(i);
}

vector<int>rets;
for (int x: nums1)
{
if (nextGreater.find(x)==nextGreater.end())
rets.push_back(-1);
else
rets.push_back(nextGreater[x]);
}
return rets;
}
};

0 comments on commit 4523734

Please sign in to comment.