Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ade4064

Browse files
committed
Add solution #456
1 parent 3cc4f69 commit ade4064

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

0456-132-pattern.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 456. 132 Pattern
3+
* https://leetcode.com/problems/132-pattern/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i],
7+
* nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].
8+
*
9+
* Return true if there is a 132 pattern in nums, otherwise, return false.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @return {boolean}
15+
*/
16+
var find132pattern = function(nums) {
17+
const stack = [];
18+
19+
for (let i = nums.length - 1, j = -Infinity; i >= 0; i--) {
20+
while (nums[i] > stack[stack.length - 1]) {
21+
j = stack.pop();
22+
}
23+
if (nums[i] < j) {
24+
return true;
25+
}
26+
stack.push(nums[i]);
27+
}
28+
29+
return false;
30+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
209209
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
210210
452|[Minimum Number of Arrows to Burst Balloons](./0452-minimum-number-of-arrows-to-burst-balloons.js)|Medium|
211+
456|[132 Pattern](./0456-132-pattern.js)|Medium|
211212
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
212213
461|[Hamming Distance](./0461-hamming-distance.js)|Easy|
213214
463|[Island Perimeter](./0463-island-perimeter.js)|Medium|

0 commit comments

Comments
 (0)