|
3 | 3 | import java.util.Stack;
|
4 | 4 |
|
5 | 5 | /**
|
| 6 | + * 503. Next Greater Element II |
| 7 | + * |
6 | 8 | * Given a circular array (the next element of the last element is the first element of the array),
|
7 | 9 | * print the Next Greater Number for every element.
|
8 | 10 | * The Next Greater Number of a number x is the first greater number to its traversing-order next in the array,
|
|
18 | 20 | */
|
19 | 21 | public class _503 {
|
20 | 22 |
|
21 |
| - //Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution |
22 |
| - //Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top |
23 |
| - public int[] nextGreaterElements(int[] nums) { |
24 |
| - if (nums == null || nums.length == 0) { |
25 |
| - return nums; |
26 |
| - } |
27 |
| - int len = nums.length; |
28 |
| - Stack<Integer> stack = new Stack<>(); |
29 |
| - for (int i = len - 1; i >= 0; i--) { |
30 |
| - stack.push(i); |
31 |
| - //push all indexes into the stack reversely |
32 |
| - } |
33 |
| - int[] result = new int[len]; |
34 |
| - for (int i = len - 1; i >= 0; i--) { |
35 |
| - result[i] = -1; |
36 |
| - //initialize it to be -1 in case we cannot find its next greater element in the array |
37 |
| - while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) { |
38 |
| - stack.pop(); |
| 23 | + public static class Solution1 { |
| 24 | + /** |
| 25 | + * Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution |
| 26 | + * Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top |
| 27 | + */ |
| 28 | + public int[] nextGreaterElements(int[] nums) { |
| 29 | + if (nums == null || nums.length == 0) { |
| 30 | + return nums; |
| 31 | + } |
| 32 | + int len = nums.length; |
| 33 | + Stack<Integer> stack = new Stack<>(); |
| 34 | + for (int i = len - 1; i >= 0; i--) { |
| 35 | + stack.push(i); |
| 36 | + //push all indexes into the stack reversely |
39 | 37 | }
|
40 |
| - if (!stack.isEmpty()) { |
41 |
| - result[i] = nums[stack.peek()]; |
| 38 | + int[] result = new int[len]; |
| 39 | + for (int i = len - 1; i >= 0; i--) { |
| 40 | + result[i] = -1; |
| 41 | + //initialize it to be -1 in case we cannot find its next greater element in the array |
| 42 | + while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) { |
| 43 | + stack.pop(); |
| 44 | + } |
| 45 | + if (!stack.isEmpty()) { |
| 46 | + result[i] = nums[stack.peek()]; |
| 47 | + } |
| 48 | + stack.push(i); |
42 | 49 | }
|
43 |
| - stack.push(i); |
| 50 | + return result; |
44 | 51 | }
|
45 |
| - return result; |
46 | 52 | }
|
47 | 53 |
|
48 |
| - //credit: https://leetcode.com/articles/next-greater-element-ii/ |
49 |
| - public int[] nextGreaterElements_editorial_solution(int[] nums) { |
50 |
| - int[] result = new int[nums.length]; |
51 |
| - Stack<Integer> stack = new Stack<>(); |
52 |
| - for (int i = nums.length * 2 - 1; i >= 0; i--) { |
53 |
| - while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) { |
54 |
| - stack.pop(); |
| 54 | + public static class Solution2 { |
| 55 | + /** |
| 56 | + * credit: https://leetcode.com/articles/next-greater-element-ii/ |
| 57 | + */ |
| 58 | + public int[] nextGreaterElements(int[] nums) { |
| 59 | + int[] result = new int[nums.length]; |
| 60 | + Stack<Integer> stack = new Stack<>(); |
| 61 | + for (int i = nums.length * 2 - 1; i >= 0; i--) { |
| 62 | + while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) { |
| 63 | + stack.pop(); |
| 64 | + } |
| 65 | + result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()]; |
| 66 | + stack.push(i % nums.length); |
55 | 67 | }
|
56 |
| - result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()]; |
57 |
| - stack.push(i % nums.length); |
| 68 | + return result; |
58 | 69 | }
|
59 |
| - return result; |
60 | 70 | }
|
61 | 71 |
|
62 | 72 | }
|
0 commit comments