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