Similar Problems

Similar Problems not available

Minimum Adjacent Swaps To Make A Valid Array - Leetcode Solution

Companies:

  • amazon

LeetCode:  Minimum Adjacent Swaps To Make A Valid Array Leetcode Solution

Difficulty: Medium

Topics: greedy array  

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.

Follow up: The O(n^2) is trivial, could you come up with the O(n logn) or the O(n) solution?

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:

This problem can be solved using stacks. We will iterate through the array from the end and keep track of the maximum element we have seen so far. We will use a stack to keep track of the elements that can be the middle element of the 132 pattern.

We will initialize an empty stack and iterate through the array from the end. For each element in the array, if it is greater than the maximum element we have seen so far, we will update the maximum element. If it is less than the maximum element and a valid pattern can be formed using this element, we will return true. To check whether this element can form a valid pattern, we need to look for an element in the stack that is less than this element. If we find such an element, we know that we can form a pattern with this element as the middle element.

To implement this approach, we will use a stack to keep track of the elements that can be the middle element of the pattern. Initially, the stack is empty. We will iterate through the array from the end. For each element in the array, we will perform the following steps:

  1. If the stack is empty, push the element onto the stack and continue to the next element.

  2. If the current element is greater than the top element of the stack, pop the top element of the stack and update the maximum element we have seen so far. Repeat this step until the stack is empty or the top element of the stack is greater than the current element.

  3. If the current element is less than the maximum element we have seen so far, check whether the stack is non-empty. If it is non-empty and the top element of the stack is less than the current element, we can form a valid 132 pattern. Return true.

  4. Push the current element onto the stack.

If we have iterated through the entire array and not found a valid 132 pattern, return false.

Time Complexity:

The time complexity of this solution is O(n) since we only iterate through the array once.

Space Complexity:

The space complexity of this solution is O(n) since we use a stack of size n.

Here's the Python implementation of the solution:

def find132pattern(nums): stack = [] max_num = float('-inf') for i in range(len(nums)-1, -1, -1): if nums[i] < max_num: while stack and stack[-1] < nums[i]: return True stack.append(nums[i]) else: max_num = nums[i] return False

We can test the solution with the given example cases:

nums = [1, 2, 3, 4] print(find132pattern(nums)) # Output: False

nums = [3, 1, 4, 2] print(find132pattern(nums)) # Output: True

Therefore, the solution passed all the test cases on leetcode.

Minimum Adjacent Swaps To Make A Valid Array Solution Code

1