Skip to content

Commit 5a82e2f

Browse files
authored
Merge pull request #1982 from alexprudhomme/0496-next-greater-element-i.cs
Create: 0496-next-greater-element-i.cs
2 parents 10b8c6f + 1632da0 commit 5a82e2f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int[] NextGreaterElement(int[] nums1, int[] nums2) {
3+
4+
Dictionary<int,int> dic = new Dictionary<int,int>();
5+
Stack<int> stack = new Stack<int>();
6+
foreach(var num in nums2)
7+
{
8+
while(stack.Count > 0 && num > stack.Peek())
9+
dic.Add(stack.Pop(),num);
10+
11+
stack.Push(num);
12+
}
13+
14+
int[] res = new int[nums1.Length];
15+
for(int i = 0; i < nums1.Length; i++)
16+
res[i] = dic.ContainsKey(nums1[i])? dic[nums1[i]] : -1;
17+
18+
return res;
19+
}
20+
}

0 commit comments

Comments
 (0)