Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 10. 11. 23:45

https://leetcode.com/problems/increasing-triplet-subsequence/

 

Increasing Triplet Subsequence - 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

뒤에서부터 순회하면 될 것 같았는데 N이 좀 큰편이라 N^3이라서 TL 남 =_=;
그래서 솔루션 보고 제출함.

# 내가 작성한 코드
class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        cnt = 1        
        for i in range(len(nums)-1, -1, -1):
            for j in range(i-1, -1, -1):
                for k in range(j-1, -1, -1):
                    if nums[k] < nums[j] < nums[i]:
                        return True
                
        return False

# 솔루션 코드
class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        first, second = float('inf'), float('inf')
        
        for n in nums:
            if n < first:
                first = n
            elif n < second and n > first:
                second = n
            
            if n > first and n > second:
                return True
        
        return False
반응형