Leetcode 456: 132 Pattern
Given an array of integers, determine if there exists a subsequence of three integers nums[i], nums[j], nums[k] where i < j < k and nums[i] < nums[k] < nums[j]. Return true if such a subsequence exists, otherwise return false.
Problem
Approach
Steps
Complexity
Input: The input is an array of integers.
Example: nums = [4, 1, 5, 3]
Constraints:
• 1 <= n <= 2 * 10^5
• -10^9 <= nums[i] <= 10^9
Output: Return a boolean indicating whether a 132 subsequence exists in the array.
Example: Output: true
Constraints:
• The result should be a boolean indicating whether the pattern exists.
Goal: The goal is to check for the existence of a subsequence that satisfies the condition nums[i] < nums[k] < nums[j].
Steps:
• 1. Iterate through the array from right to left, keeping track of a potential 132 pattern.
• 2. Use a stack to keep track of numbers and find a valid pattern by checking if nums[i] < nums[k] < nums[j].
Goal: Handle large inputs efficiently with O(n) complexity.
Steps:
• The array length can be as large as 200,000.
• Each element of the array can be as large as 10^9 or as small as -10^9.
Assumptions:
• The input array is not empty.
• Input: nums = [1, 2, 3, 4]
• Explanation: No valid subsequence exists because the elements are in increasing order, which doesn't form a 132 pattern.
• Input: nums = [6, 3, 4, 1]
• Explanation: A valid 132 pattern exists in the subsequence [3, 4, 1], where 3 < 1 < 4.
Approach: We can use a stack to efficiently check if a valid 132 subsequence exists, iterating through the array from right to left.
Observations:
• We need to check for the pattern nums[i] < nums[k] < nums[j] efficiently.
• The stack can help by maintaining the potential nums[k] and allowing quick comparisons for nums[i].
Steps:
• 1. Start from the rightmost element and iterate backwards.
• 2. Maintain a stack of numbers where each number is greater than the one before it.
• 3. Check if the current element is smaller than the top of the stack (potential nums[k]). If it is, return true.
Empty Inputs:
• If the array has less than 3 elements, the answer is false.
Large Inputs:
• Handle large arrays efficiently, making sure the solution scales with input size up to 2 * 10^5.
Special Values:
• Arrays with only increasing or decreasing values should return false.
Constraints:
• Ensure the solution runs in O(n) time for large inputs.
bool find132pattern(vector<int>& nums) {
int n = nums.size();
int prv = INT_MIN;
stack<int> stk;
for(int i = n - 1; i >= 0; i--) {
if (nums[i] < prv) return true;
while(!stk.empty() && nums[i] > stk.top()) {
prv = stk.top();
stk.pop();
}
stk.push(nums[i]);
}
return false;
}
1 : Function Definition
bool find132pattern(vector<int>& nums) {
Defines the `find132pattern` function that takes a vector `nums` and returns a boolean indicating whether a 132 pattern exists in the array.
2 : Variable Initialization
int n = nums.size();
Initializes variable `n` to store the size of the input array `nums`.
3 : Variable Initialization
int prv = INT_MIN;
Initializes the variable `prv` to `INT_MIN`, which will be used to store the potential third element of the 132 pattern.
4 : Data Structure Initialization
stack<int> stk;
Initializes a stack `stk` to help track the second element of the 132 pattern and assist in checking the 132 pattern.
5 : Loop
for(int i = n - 1; i >= 0; i--) {
Starts a loop that iterates over the array `nums` from the last element to the first, checking potential 132 patterns in reverse.
6 : Pattern Check
if (nums[i] < prv) return true;
Checks if the current element `nums[i]` is smaller than `prv` (which represents the third element of the pattern). If true, a 132 pattern is found and the function returns `true`.
7 : While Loop
while(!stk.empty() && nums[i] > stk.top()) {
Enters a while loop to pop elements from the stack `stk` as long as the current element `nums[i]` is larger than the top of the stack, updating `prv` to the popped values.
8 : Stack Operations
prv = stk.top();
Updates the value of `prv` to the top element of the stack, representing the second element of a potential 132 pattern.
9 : Stack Operations
stk.pop();
Pops the top element from the stack, as it can no longer be part of a valid 132 pattern with the current element `nums[i]`.
10 : Stack Push
}
Ends the while loop. All elements in the stack that are smaller than `nums[i]` have been removed.
11 : Stack Operations
stk.push(nums[i]);
Pushes the current element `nums[i]` onto the stack, which may be used as a potential second element in a future 132 pattern.
12 : Return Statement
return false;
Returns `false` if no 132 pattern was found after checking all elements.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) because we iterate over the array once and perform constant-time stack operations.
Best Case: O(1)
Worst Case: O(n)
Description: The space complexity is O(n) due to the stack, which stores up to n elements in the worst case.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus