Skip to content

Commit 66ff35f

Browse files
authored
Merge pull request #1925 from AkifhanIlgaz/0456
Create: 0456-132-pattern.rs / .ts / .js
2 parents 165ef04 + 9787603 commit 66ff35f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

javascript/0456-132-pattern.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var find132pattern = function (nums) {
6+
let stack = []; // [num, minLeft]
7+
let curMin = nums[0];
8+
9+
for (n of nums.slice(1)) {
10+
while (stack.length > 0 && n >= stack.at(-1)[0]) {
11+
stack.pop();
12+
}
13+
if (stack.length > 0 && n > stack.at(-1)[1]) {
14+
return true;
15+
}
16+
17+
stack.push([n, curMin]);
18+
curMin = Math.min(curMin, n);
19+
}
20+
21+
return false;
22+
};

rust/0456-132-pattern.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn find132pattern(nums: Vec<i32>) -> bool {
3+
let mut stack: Vec<(i32, i32)> = vec![]; // (num, min_left)
4+
let mut current_min = nums[0];
5+
6+
for n in nums.iter().skip(1) {
7+
while !stack.is_empty() && *n >= stack.last().unwrap().0 {
8+
stack.pop();
9+
}
10+
if !stack.is_empty() && *n > stack.last().unwrap().1 {
11+
return true;
12+
}
13+
14+
stack.push((*n, current_min));
15+
current_min = current_min.min(*n);
16+
}
17+
18+
false
19+
}
20+
}

typescript/0456-132-pattern.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function find132pattern(nums: number[]): boolean {
2+
let stack: { num: number; minLeft: number }[] = []; // [num, minLeft]
3+
let curMin = nums[0];
4+
5+
for (let n of nums.slice(1)) {
6+
while (stack.length > 0 && n >= stack[stack.length - 1].num) {
7+
stack.pop();
8+
}
9+
if (stack.length > 0 && n > stack[stack.length - 1].minLeft) {
10+
return true;
11+
}
12+
13+
stack.push({ num: n, minLeft: curMin });
14+
curMin = Math.min(curMin, n);
15+
}
16+
17+
return false;
18+
}

0 commit comments

Comments
 (0)