LeetCode 300: Longest Increasing Subsequence — 단계별 시각적 추적

발행: (2026년 4월 9일 오후 03:38 GMT+9)
2 분 소요
원문: Dev.to

Source: Dev.to

Problem Statement

정수 배열에서 가장 긴 엄격히 증가하는 부분 수열의 길이를 찾으세요. 부분 수열은 원소들의 상대적 순서를 유지하지만 연속될 필요는 없습니다.

Approach

동적 프로그래밍을 사용합니다. dp[i]는 인덱스 i에서 끝나는 가장 긴 증가 부분 수열의 길이를 나타냅니다.
각 원소에 대해 이전 원소들을 모두 확인하고, 현재 원소가 더 크면 그 부분 수열을 확장합니다.

Complexity

  • Time:O(n²)
  • Space:O(n)

Code

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        if not nums:
            return 0

        # Initialize a dynamic programming array dp with all values set to 1.
        dp = [1] * len(nums)

        # Iterate through the array to find the longest increasing subsequence.
        for i in range(len(nums)):
            for j in range(i):
                if nums[i] > nums[j]:
                    dp[i] = max(dp[i], dp[j] + 1)

        # Return the maximum value in dp, which represents the length of the longest increasing subsequence.
        return max(dp)

Visualization

인터랙티브 시각화 열기: Open TraceLit and step through every line.
TraceLit으로 제작되었습니다 — LeetCode 연습을 위한 시각적 알고리즘 트레이서.

0 조회
Back to Blog

관련 글

더 보기 »