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.
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].
Constraints:
n == nums.length1 <= n <= 2 * 105-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding a 132 pattern is like trying every possible combination. We're searching for three numbers where the first is smaller than the third, and the third is smaller than the second. We check every single group of three numbers to see if they fit this pattern.
Here's how the algorithm would work step-by-step:
def has_132_pattern_brute_force(numbers):
list_length = len(numbers)
for first_index in range(list_length):
for second_index in range(first_index + 1, list_length):
# Need to check for third numbers after the second number.
for third_index in range(second_index + 1, list_length):
# Check if the 132 pattern exists
if numbers[first_index] < numbers[third_index] and numbers[third_index] < numbers[second_index]:
return True
return FalseThe core idea is to efficiently identify the '3' in the 132 pattern. We maintain a stack of potential '2' values, ensuring that we check for the pattern in reverse order which allows for efficient discovery.
Here's how the algorithm would work step-by-step:
def find132pattern(numbers) -> bool:
stack_of_potential_second_values = []
maximum_second_value = float('-inf')
for i in range(len(numbers) - 1, -1, -1):
# Check if the current number can be the '1'
if numbers[i] < maximum_second_value:
return True
# Maintain a decreasing stack for potential '2' values
while stack_of_potential_second_values and numbers[i] > stack_of_potential_second_values[-1]:
# Update max_second_value with viable stack values
maximum_second_value = stack_of_potential_second_values.pop()
stack_of_potential_second_values.append(numbers[i])
return False| Case | How to Handle |
|---|---|
| Empty or null input array | Return false immediately since a 132 pattern requires at least three elements. |
| Array with less than three elements | Return false immediately as a 132 pattern cannot exist with fewer than three elements. |
| Array with all elements identical | Return false because nums[i] < nums[k] < nums[j] cannot be satisfied with identical elements. |
| Array sorted in ascending order | Return false, as it's impossible to find nums[i] < nums[k] < nums[j] where i < j < k. |
| Array sorted in descending order | Iterate through the array, the middle number must be largest, checking previous and succeeding values for condition. |
| Array with large range of integer values (positive and negative) | The algorithm should handle both positive and negative integers correctly without overflow issues. |
| Very large input array (performance) | The algorithm should have a time complexity better than O(n^3) to avoid exceeding time limits with large inputs, preferably O(n) or O(n log n). |
| Input contains integer overflow condition with mathematical operations. | Avoid potential integer overflow issues by not performing calculations that could exceed the maximum integer value, or use a larger data type like long if necessary. |