파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 7. 23:45

https://leetcode.com/problems/132-pattern

 

132 Pattern - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

이것도 뒤에서 부터 순회하면서 스텍 사용해서 풀면 됨

class Solution:
    def find132pattern(self, nums: List[int]) -> bool:
        if len(nums)<3:
            return False
      
        second_num = -math.inf
        stck = []
        
        for i in range(len(nums) - 1, -1, -1):
            if nums[i] < second_num:
                return True
            while stck and stck[-1] < nums[i]:
                second_num = stck.pop()
                
            stck.append(nums[i])
        return False
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.05.09
Today's Challenge  (0) 2022.05.08
Today's Challenge  (0) 2022.05.06
Today's Challenge  (0) 2022.05.05
Today's Challenge  (0) 2022.05.04
Comments