-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
456. 132 Pattern - Python
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# LeetCode 456. | ||
|
||
## Problem Statement | ||
Given an array of n integers nums, a 132 pattern is a subsequence of three integers | ||
nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. | ||
Return true if there is a 132 pattern in nums, otherwise return false. | ||
|
||
### Test Cases | ||
|
||
#### Example 1: | ||
- Input: nums = [1,2,3,4] | ||
- Output: false | ||
- Explanation: There is no 132 pattern in the sequence. | ||
|
||
#### Example 2: | ||
- Input: nums = [3,1,4,2] | ||
- Output: true | ||
- Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. | ||
|
||
## Solution | ||
We iterate through the 'nums' list in reverse order. We maintain a stack to keep track of potential '2' candidates. | ||
We also keep track of the '3' candidate as 'third'. If we encounter a number smaller than 'third', we return True as we found a '132' pattern. | ||
If the current number is greater than the top of the stack, it becomes a potential '3' candidate, and we update 'third'. | ||
Finally, we push the current number onto the stack for potential future '2' candidates. | ||
|
||
### Complexity | ||
- Time complexity: O(n), where n is the length of the 'nums' list. | ||
- Space complexity: O(n), as the stack can potentially hold all elements of the 'nums' list. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
''' | ||
* LeetCode 456. | ||
* Problem Statement- Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. | ||
Return true if there is a 132 pattern in nums, otherwise, return false. | ||
* | ||
* Test Cases: | ||
* Example 1: | ||
Input: nums = [1,2,3,4] | ||
Output: false | ||
Explanation: There is no 132 pattern in the sequence. | ||
* Example 2: | ||
Input: nums = [3,1,4,2] | ||
Output: true | ||
Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. | ||
* Example 3: | ||
Input: nums = [-1,3,2,0] | ||
Output: true | ||
Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. | ||
''' | ||
|
||
# Solution | ||
class Solution: | ||
def find132pattern(self, nums: List[int]) -> bool: | ||
# Initialize an empty stack and set 'third' to negative infinity. | ||
stack, third = [], float('-inf') | ||
|
||
# Iterate through the 'nums' list in reverse order. | ||
for num in reversed(nums): | ||
# If the current number is smaller than 'third', we found the '132' pattern. | ||
if num < third: | ||
return True | ||
|
||
# While the stack is not empty and the top of the stack is smaller than the current number, | ||
# pop elements from the stack and update 'third' if necessary. | ||
while stack and stack[-1] < num: | ||
third = stack.pop() | ||
|
||
# Push the current number onto the stack for potential future comparisons. | ||
stack.append(num) | ||
|
||
# If no '132' pattern is found after iterating through the entire list, return False. | ||
return False | ||
|