|
10 | 10 | * https://www.geeksforgeeks.org/dsa/introduction-to-monotonic-stack-2/
|
11 | 11 | */
|
12 | 12 |
|
| 13 | +package com.thealgorithms.stacks; |
| 14 | + |
13 | 15 | import java.util.Stack;
|
14 | 16 |
|
15 | 17 | public final class MonotonicIncreasingStack {
|
16 | 18 |
|
17 |
| - private MonotonicIncreasingStack() { |
18 |
| - throw new AssertionError("Cannot instantiate utility class"); |
19 |
| - } |
| 19 | + private MonotonicIncreasingStack() { |
| 20 | + throw new AssertionError("Cannot instantiate utility class"); |
| 21 | + } |
20 | 22 |
|
21 |
| - /** |
22 |
| - * Finds the next greater element for each element in the given array. |
23 |
| - * |
24 |
| - * For each element, it returns the nearest greater element to its right. |
25 |
| - * If no such element exists, -1 is returned for that index. |
26 |
| - * |
27 |
| - * Time Complexity: O(n) |
28 |
| - * Space Complexity: O(n) |
29 |
| - */ |
30 |
| - public static int[] nextGreaterElement(int[] arr) { |
31 |
| - int n = arr.length; |
32 |
| - int[] result = new int[n]; |
33 |
| - Stack<Integer> stack = new Stack<>(); |
| 23 | + /** |
| 24 | + * Finds the next greater element for each element in the given array. |
| 25 | + * For each element, it returns the nearest greater element to its right. |
| 26 | + * If no such element exists, -1 is returned for that index. |
| 27 | + * |
| 28 | + * Time Complexity: O(n) |
| 29 | + * Space Complexity: O(n) |
| 30 | + */ |
| 31 | + public static int[] nextGreaterElement(int[] arr) { |
| 32 | + int n = arr.length; |
| 33 | + int[] result = new int[n]; |
| 34 | + Stack<Integer> stack = new Stack<>(); |
34 | 35 |
|
35 |
| - for (int i = n - 1; i >= 0; i--) { |
36 |
| - while (!stack.isEmpty() && arr[i] >= arr[stack.peek()]) { |
37 |
| - stack.pop(); |
38 |
| - } |
39 |
| - result[i] = stack.isEmpty() ? -1 : arr[stack.peek()]; |
40 |
| - stack.push(i); |
41 |
| - } |
42 |
| - return result; |
| 36 | + for (int i = n - 1; i >= 0; i--) { |
| 37 | + while (!stack.isEmpty() && arr[i] >= arr[stack.peek()]) { |
| 38 | + stack.pop(); |
| 39 | + } |
| 40 | + result[i] = stack.isEmpty() ? -1 : arr[stack.peek()]; |
| 41 | + stack.push(i); |
43 | 42 | }
|
| 43 | + return result; |
| 44 | + } |
44 | 45 |
|
45 |
| - /** |
46 |
| - * Finds the next smaller element for each element in the given array. |
47 |
| - * |
48 |
| - * For each element, it returns the nearest smaller element to its right. |
49 |
| - * If no such element exists, -1 is returned for that index. |
50 |
| - * |
51 |
| - * Time Complexity: O(n) |
52 |
| - * Space Complexity: O(n) |
53 |
| - */ |
54 |
| - public static int[] nextSmallerElement(int[] arr) { |
55 |
| - int n = arr.length; |
56 |
| - int[] result = new int[n]; |
57 |
| - Stack<Integer> stack = new Stack<>(); |
| 46 | + /** |
| 47 | + * Finds the next smaller element for each element in the given array. |
| 48 | + * For each element, it returns the nearest smaller element to its right. |
| 49 | + * If no such element exists, -1 is returned for that index. |
| 50 | + * |
| 51 | + * Time Complexity: O(n) |
| 52 | + * Space Complexity: O(n) |
| 53 | + */ |
| 54 | + public static int[] nextSmallerElement(int[] arr) { |
| 55 | + int n = arr.length; |
| 56 | + int[] result = new int[n]; |
| 57 | + Stack<Integer> stack = new Stack<>(); |
58 | 58 |
|
59 |
| - for (int i = n - 1; i >= 0; i--) { |
60 |
| - while (!stack.isEmpty() && arr[i] <= arr[stack.peek()]) { |
61 |
| - stack.pop(); |
62 |
| - } |
63 |
| - result[i] = stack.isEmpty() ? -1 : arr[stack.peek()]; |
64 |
| - stack.push(i); |
65 |
| - } |
66 |
| - return result; |
| 59 | + for (int i = n - 1; i >= 0; i--) { |
| 60 | + while (!stack.isEmpty() && arr[i] <= arr[stack.peek()]) { |
| 61 | + stack.pop(); |
| 62 | + } |
| 63 | + result[i] = stack.isEmpty() ? -1 : arr[stack.peek()]; |
| 64 | + stack.push(i); |
67 | 65 | }
|
| 66 | + return result; |
| 67 | + } |
68 | 68 |
|
69 |
| - // Test class included in the same file |
70 |
| - public static void main(String[] args) { |
71 |
| - testScenario(new int[]{2, 5, 1, 3, 4}); |
72 |
| - testScenario(new int[]{1, 2, 3, 4, 5}); |
73 |
| - testScenario(new int[]{5, 4, 3, 2, 1}); |
74 |
| - } |
| 69 | + // Test class included in the same file |
| 70 | + public static void main(String[] args) { |
| 71 | + testScenario(new int[]{2, 5, 1, 3, 4}); |
| 72 | + testScenario(new int[]{1, 2, 3, 4, 5}); |
| 73 | + testScenario(new int[]{5, 4, 3, 2, 1}); |
| 74 | + } |
75 | 75 |
|
76 |
| - private static void testScenario(int[] arr) { |
77 |
| - int[] nextGreater = nextGreaterElement(arr); |
78 |
| - int[] nextSmaller = nextSmallerElement(arr); |
| 76 | + private static void testScenario(int[] arr) { |
| 77 | + int[] nextGreater = nextGreaterElement(arr); |
| 78 | + int[] nextSmaller = nextSmallerElement(arr); |
79 | 79 |
|
80 |
| - System.out.print("Array: "); |
81 |
| - printArray(arr); |
82 |
| - System.out.print("Next Greater: "); |
83 |
| - printArray(nextGreater); |
84 |
| - System.out.print("Next Smaller: "); |
85 |
| - printArray(nextSmaller); |
86 |
| - System.out.println("-----------------------------"); |
87 |
| - } |
| 80 | + System.out.print("Array: "); |
| 81 | + printArray(arr); |
| 82 | + System.out.print("Next Greater: "); |
| 83 | + printArray(nextGreater); |
| 84 | + System.out.print("Next Smaller: "); |
| 85 | + printArray(nextSmaller); |
| 86 | + System.out.println("-----------------------------"); |
| 87 | + } |
88 | 88 |
|
89 |
| - private static void printArray(int[] arr) { |
90 |
| - for (int n : arr) System.out.print(n + " "); |
91 |
| - System.out.println(); |
| 89 | + private static void printArray(int[] arr) { |
| 90 | + for (int n : arr) { |
| 91 | + System.out.print(n + " "); |
92 | 92 | }
|
| 93 | + System.out.println(); |
| 94 | + } |
93 | 95 | }
|
0 commit comments