Skip to content

Commit

Permalink
solution(python): 456. 132 Pattern
Browse files Browse the repository at this point in the history
456. 132 Pattern
- Python
  • Loading branch information
godkingjay authored Oct 16, 2023
2 parents 4fe9a90 + 8ee3bc0 commit 9d283e9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Medium/456. 132 Pattern/README.md
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.
46 changes: 46 additions & 0 deletions Medium/456. 132 Pattern/sol.py
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

0 comments on commit 9d283e9

Please sign in to comment.