File tree 1 file changed +10
-10
lines changed
1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change @@ -3,21 +3,21 @@ public boolean find132pattern(int[] nums) {
3
3
if (nums .length < 3 ) {
4
4
return false ;
5
5
}
6
- Stack <Integer > stack = new Stack <>();
7
- int [] min = new int [nums .length ];
8
- min [0 ] = nums [0 ];
9
- for (int i = 1 ; i < nums .length ; i ++) {
10
- min [i ] = Math .min (min [i - 1 ], nums [i ]);
6
+ int [] minTillIndex = new int [nums .length ];
7
+ minTillIndex [0 ] = nums [0 ];
8
+ for (int idx = 1 ; idx < nums .length ; idx ++) {
9
+ minTillIndex [idx ] = Math .min (minTillIndex [idx - 1 ], nums [idx ]);
11
10
}
12
- for (int i = nums .length - 1 ; i >= 0 ; i --) {
13
- if (nums [i ] > min [i ]) { // Consider nums[i] as 3 and min[i] as 1
14
- while (!stack .isEmpty () && stack .peek () <= min [i ]) { // Search for 2
11
+ Stack <Integer > stack = new Stack <>();
12
+ for (int idx = nums .length - 1 ; idx >= 0 ; idx --) {
13
+ if (nums [idx ] > minTillIndex [idx ]) {
14
+ while (!stack .isEmpty () && stack .peek () <= minTillIndex [idx ]) {
15
15
stack .pop ();
16
16
}
17
- if (!stack .isEmpty () && stack .peek () < nums [i ]) { // If 2 found
17
+ if (!stack .isEmpty () && stack .peek () < nums [idx ]) {
18
18
return true ;
19
19
}
20
- stack .push (nums [i ]);
20
+ stack .push (nums [idx ]);
21
21
}
22
22
}
23
23
return false ;
You can’t perform that action at this time.
0 commit comments