Skip to content

Commit 5a82e2f

Browse files
authored
Merge pull request neetcode-gh#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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy