-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-greater-element-i.java
54 lines (41 loc) · 1.14 KB
/
next-greater-element-i.java
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
47
48
49
50
51
52
53
54
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> st=new Stack<>();
int[] ans=new int[nums2.length];
st.push(nums2[nums2.length-1]);
ans[ans.length-1]=-1;
for(int i=nums2.length-2;i>=0;i--)
{
if(nums2[i]>st.peek())
{
while(!st.isEmpty()&&st.peek()<nums2[i])
{
st.pop();
}
ans[i]=st.size()==0?-1:st.peek();
st.push(nums2[i]);
}
else
{
ans[i]=st.size()==0?-1:st.peek();
st.push(nums2[i]);
}
}
int[] result=new int[nums1.length];
for(int i=0;i<nums1.length;i++)
{
int t= search(nums1[i],nums2);
result[i]=ans[t];
}
return result;
}
int search(int target,int[] nums2)
{
for(int i=0;i<nums2.length;i++)
{
if(nums2[i]==target)
return i;
}
return -1;
}
}