Scribbling

LeetCode: 334. Increasing Triplet Subsequence 본문

Computer Science/Coding Test

LeetCode: 334. Increasing Triplet Subsequence

focalpoint 2022. 1. 11. 20:38

This problem is a special case of #300, which is LIS.

Refer to LIS algorithm here.

https://focalpoint.tistory.com/179

 

LeetCode: 300. Longest Increasing Subsequence - 시바견의 끄적임

1. 가장 간단한 DP 형태 O(N**2)이다. class Solution: def lengthOfLIS(self, nums: List[int]) -> int: dp = [0] * len(nums) dp[0] = 1 for i in range(1, len(nums)): temp = -1 j = i - 1 while j >= 0: if n..

focalpoint.tistory.com

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        dp = [nums[0]]
        for i in range(1, len(nums)):
            num = nums[i]
            insert_flag = False
            for j in range(len(dp)):
                if num <= dp[j]:
                    dp[j] = num
                    insert_flag = True
                    break
            if not insert_flag:
                dp.append(num)
            if len(dp) == 3:
                return True
        return False