DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

132 Pattern

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.length
  • 1 <= n <= 2 * 105
  • -109 <= nums[i] <= 109

SOLUTION:

class Solution:
    def find132pattern(self, nums: List[int]) -> bool:
        n = len(nums)
        if n < 3:
            return False
        stack = []
        k = -1
        for i in range(n - 1, -1, -1):
            if k > -1 and nums[k] > nums[i]:
                return True
            while len(stack) > 0 and nums[i] > nums[stack[-1]]:
                k = stack.pop()
            stack.append(i)
        return False

# class Solution:
#     def makeSeg(self, arr, i, j):
#         seg = self.seg
#         if (i, j) in seg:
#             return seg[(i, j)]
#         if i == j:
#             seg[(i, j)] = arr[i]
#             return arr[i]
#         mid = (i + j) // 2
#         curr = max(self.makeSeg(arr, i, mid), self.makeSeg(arr, mid + 1, j))
#         seg[(i, j)] = curr
#         return curr

#     def getMax(self, arr, i, j, ni, nj):
#         seg = self.seg
#         if ni >= i and nj <= j:
#             return seg[(ni, nj)]
#         if (ni < i and nj < i) or (ni > j and nj > j):
#             return float('-inf')
#         mid = (ni + nj) // 2
#         return max(self.getMax(arr, i, j, ni, mid), self.getMax(arr, i, j, mid + 1, nj))

#     def find132pattern(self, nums: List[int]) -> bool:
#         n = len(nums)
#         self.seg = {}
#         self.makeSeg(nums, 0, n - 1)
#         for i in range(n):
#             for j in range(i + 2, n):
#                 if nums[j] > nums[i] and self.getMax(nums, i, j, 0, n - 1) > nums[j]:
#                     return True
#         return False

# class Solution:
#     def find132pattern(self, nums: List[int]) -> bool:
#         n = len(nums)
#         next_lowest = [n for i in range(n)]
#         stack = []
#         for i in range(n):
#             while len(stack) > 0 and nums[i] < nums[stack[-1]]:
#                 curr = stack.pop()
#                 next_lowest[curr] = i
#                 for j in range(curr):
#                     if nums[j] < nums[i]:
#                         return True
#             stack.append(i)
#         return False

# class Solution:
#     def find132pattern(self, nums: List[int]) -> bool:
#         currMin = float('inf')
#         currMax = float('-inf')
#         for num in nums:
#             currMin = min(currMin, num)
#             currMax = max(currMax, num)
#             if num > currMin and num < currMax:
#                 return True
#         return False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)