1
+ package stack .next_greater_element_i ;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
7
+
8
+ /**
9
+ * Test suite for the {@link MonotonicStack} class.
10
+ */
11
+ class MonotonicStackTest {
12
+
13
+ private final MonotonicStack monotonicStack = new MonotonicStack ();
14
+
15
+ @ Test
16
+ @ DisplayName ("Should pass the primary LeetCode example" )
17
+ void testLeetCodeExample1 () {
18
+ // Arrange
19
+ int [] nums1 = {4 , 1 , 2 };
20
+ int [] nums2 = {1 , 3 , 4 , 2 };
21
+ int [] expected = {-1 , 3 , -1 };
22
+
23
+ // Act
24
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
25
+
26
+ // Assert
27
+ assertArrayEquals (expected , result , "Should find the correct next greater elements" );
28
+ }
29
+
30
+ @ Test
31
+ @ DisplayName ("Should pass the second LeetCode example" )
32
+ void testLeetCodeExample2 () {
33
+ // Arrange
34
+ int [] nums1 = {2 , 4 };
35
+ int [] nums2 = {1 , 2 , 3 , 4 };
36
+ int [] expected = {3 , -1 };
37
+
38
+ // Act
39
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
40
+
41
+ // Assert
42
+ assertArrayEquals (expected , result , "Should handle cases where the element is at the end" );
43
+ }
44
+
45
+ @ Test
46
+ @ DisplayName ("Should return -1 for all elements when nums2 is strictly decreasing" )
47
+ void testStrictlyDecreasingArray () {
48
+ // Arrange
49
+ int [] nums1 = {5 , 3 , 1 };
50
+ int [] nums2 = {5 , 4 , 3 , 2 , 1 };
51
+ int [] expected = {-1 , -1 , -1 };
52
+
53
+ // Act
54
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
55
+
56
+ // Assert
57
+ assertArrayEquals (expected , result , "All results should be -1 for a decreasing array" );
58
+ }
59
+
60
+ @ Test
61
+ @ DisplayName ("Should find the adjacent element when nums2 is strictly increasing" )
62
+ void testStrictlyIncreasingArray () {
63
+ // Arrange
64
+ int [] nums1 = {1 , 2 , 3 , 4 };
65
+ int [] nums2 = {1 , 2 , 3 , 4 , 5 };
66
+ int [] expected = {2 , 3 , 4 , 5 };
67
+
68
+ // Act
69
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
70
+
71
+ // Assert
72
+ assertArrayEquals (expected , result , "Next greater element should be the next one in sequence" );
73
+ }
74
+
75
+ @ Test
76
+ @ DisplayName ("Should work correctly when nums1 is a permutation of nums2" )
77
+ void testWhenNums1IsPermutationOfNums2 () {
78
+ // Arrange
79
+ int [] nums1 = {1 , 3 , 5 , 2 , 4 };
80
+ int [] nums2 = {1 , 3 , 5 , 2 , 4 };
81
+ int [] expected = {3 , 5 , -1 , 4 , -1 };
82
+
83
+ // Act
84
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
85
+
86
+ // Assert
87
+ assertArrayEquals (expected , result , "Should handle the case where nums1 and nums2 are the same" );
88
+ }
89
+
90
+ @ Test
91
+ @ DisplayName ("Should handle a more complex, mixed scenario" )
92
+ void testComplexScenario () {
93
+ // Arrange
94
+ int [] nums1 = {13 , 7 , 6 , 12 };
95
+ int [] nums2 = {1 , 3 , 7 , 4 , 6 , 9 , 13 , 12 , 11 };
96
+ // NGE map for nums2: {1:3, 3:7, 7:9, 4:6, 6:9, 9:13, 13:-1, 12:-1, 11:-1}
97
+ int [] expected = {-1 , 9 , 9 , -1 };
98
+
99
+ // Act
100
+ int [] result = monotonicStack .nextGreaterElement (nums1 , nums2 );
101
+
102
+ // Assert
103
+ assertArrayEquals (expected , result , "Should correctly process a complex sequence" );
104
+ }
105
+ }
0 commit comments