132 Pattern LeetCode solution in JavaScript

1
var find132pattern = function (nums) {
2
let m = -Infinity;
3
// Initialise a empty stack...
4
const stack = [];
5
// Run a Loop from last to first index...
6
for (let i = nums.length - 1; i >= 0; i--) {
7
// If nums[i] is greater than the top element of stack, then pop the element...
8
while (nums[i] > stack[stack.length - 1]) {
9
m = stack.pop();
10
}
11
// If m is greater than nums[i], return true...
12
if (nums[i] < m) {
13
return true;
14
}
15
// Otherwise, push nums[i] into stack...
16
stack.push(nums[i]);
17
}
18
// If the condition is not satisfied, return false.
19
return false;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0