Skip to content

Commit 1632da0

Browse files
committed
Create: 0496-next-greater-element-i.cs
1 parent fa2d4c3 commit 1632da0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

csharp/0496-next-greater-element-i.cs

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)