forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update and rename 496.Next Greater Element I.cpp to 496.Next-Greater-…
…Element-I.cpp
- Loading branch information
1 parent
39507fc
commit 4523734
Showing
2 changed files
with
28 additions
and
29 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
Stack/496.Next-Greater-Element-I/496.Next Greater Element I.cpp
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
Stack/496.Next-Greater-Element-I/496.Next-Greater-Element-I.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |