-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem503.java
More file actions
37 lines (27 loc) · 853 Bytes
/
Problem503.java
File metadata and controls
37 lines (27 loc) · 853 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
package com.leetcode.problems;
import java.util.Arrays;
import java.util.Stack;
class Solution503 {
public int[] nextGreaterElements(int[] nums) {
Stack<Integer> s = new Stack<>();
int[] res = new int[nums.length];
for (int i = 2*nums.length-1; i > 0; i--) {
while (!s.empty()&&s.peek()<=nums[i%nums.length])
{
s.pop();
}
res[i%nums.length] = s.empty() ? -1 : s.peek();
s.push(nums[i%nums.length]);
}
return res;
}
}
public class Problem503 {
public static void main(String[] args) {
Solution503 s = new Solution503();
int [] nums = {2,1,2,4,3};
int[] ints = s.nextGreaterElements(nums);
System.out.println(Arrays.toString(ints));
System.out.println("hello, world");
}
}