-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathNext greater element 2
More file actions
46 lines (31 loc) · 842 Bytes
/
Next greater element 2
File metadata and controls
46 lines (31 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Solution {
public int[] NextGreaterElements(int[] nums) {
int l = nums.Length;
int[] arr = new int[2*l];
arr = nums.Concat(nums).ToArray();
int l1 = arr.Length;
int[] ans = new int[l1];
var st = new Stack<int>(){};
for(int i =0;i<l1;i++)
{
while(st.Count!=0 && arr[i]>arr[st.Peek()])
{
ans[st.Peek()] = arr[i];
st.Pop();
}
st.Push(i);
}
while(st.Count!=0)
{
ans[st.Peek()]=-1;
st.Pop();
}
var list = new List<int>(){};
for(int k=0;k<l1/2;k++)
{
list.Add(ans[k]);
}
return list.ToArray();
//return arr;
}
}