Advertisement

132 Pattern - LeetCode 456 Solution

132 Pattern - Complete Solution Guide

132 Pattern is LeetCode problem 456, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . 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

Detailed Explanation

The problem asks us to find a "132 pattern" in a given array of integers. A 132 pattern is a subsequence of three numbers, `nums[i]`, `nums[j]`, and `nums[k]`, where `i < j < k` and `nums[i] < nums[k] < nums[j]`. In simpler terms, we need to find three numbers in the array such that the first number is less than the third number, and the third number is less than the second number, and they appear in that order in the array. The input is an array of integers, and the output is a boolean value: `true` if a 132 pattern exists, and `false` otherwise. The constraints are the size of the array and the range of values within the array.

Solution Approach

The solution utilizes a stack and a variable `s2` (representing the '3' in the 132 pattern) to efficiently find the pattern. It iterates through the array from right to left. The stack stores potential '2' values (the middle number in the 132 pattern). The `s2` variable stores the largest value that has been popped from the stack so far, acting as the potential '3' value. For each number encountered (potential '1'), the solution checks if it's less than `s2`. If it is, a 132 pattern has been found. Otherwise, the stack is updated by popping elements smaller than the current number (because the current number is a better potential '2'), and the current number is then pushed onto the stack.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack to store potential '2' values and `s2` to negative infinity (or Integer.MIN_VALUE).
  2. Step 2: Iterate through the input array `nums` from right to left (from index `n-1` to `0`).
  3. Step 3: In each iteration, let `num` be the current element `nums[i]`. Check if `num` is less than `s2`. If so, return `true` (a 132 pattern is found).
  4. Step 4: While the stack is not empty and `num` is greater than the top element of the stack, pop the top element from the stack and update `s2` with the popped value. This ensures `s2` always holds the largest value popped so far, acting as the '3' in the 132 pattern.
  5. Step 5: Push `num` onto the stack. This adds `num` as a potential '2' value for future iterations.
  6. Step 6: After iterating through the entire array without finding a 132 pattern, return `false`.

Key Insights

  • Insight 1: Scanning the array from right to left allows us to maintain a potential '2' value using a stack. This helps in efficiently finding '1' such that '1 < 3 < 2'.
  • Insight 2: Using a stack (or similar data structure for C) to keep track of potential '2' values allows us to optimize the search for the '3' value. The stack helps us efficiently find the largest value less than the current number (potential '1').
  • Insight 3: The s2 variable keeps track of the largest value that has been popped from the stack, which represents a potential '3' value in the 132 pattern. If we find a '1' (nums[i]) that is less than '3' (s2), then we have found a 132 pattern.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Stack, Monotonic Stack, Ordered Set.

Companies

Asked at: IBM, Intuit.