Skip to content

Commit

Permalink
Create: 0496-next-greater-element-i.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Jan 9, 2023
1 parent fa2d4c3 commit 1632da0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions csharp/0496-next-greater-element-i.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public int[] NextGreaterElement(int[] nums1, int[] nums2) {

Dictionary<int,int> dic = new Dictionary<int,int>();
Stack<int> stack = new Stack<int>();
foreach(var num in nums2)
{
while(stack.Count > 0 && num > stack.Peek())
dic.Add(stack.Pop(),num);

stack.Push(num);
}

int[] res = new int[nums1.Length];
for(int i = 0; i < nums1.Length; i++)
res[i] = dic.ContainsKey(nums1[i])? dic[nums1[i]] : -1;

return res;
}
}

0 comments on commit 1632da0

Please sign in to comment.