300. Longest Increasing Subsequence

2025. 2. 2. 23:28·PS/LeetCode
반응형

 

문제

https://leetcode.com/problems/longest-increasing-subsequence/description/

입력

1 <= nums.length <= 2500
-10^4 <= nums[i] <= 10^4

결과

ans:Int = 최장 증가 수열길이

해석

끝지점을 하나씩 늘려가면서, 최장 수열을 기록한다.

코드

ㅇclass Solution {
    func lengthOfLIS(_ nums: [Int]) -> Int {
        let n = nums.count
        if n == 1 {
            return 1 
        }

        var cache = [Int](repeating:0, count:n)
        var ans: Int = -1

        for i in 0..<n {
            cache[i] = 1
            let now = nums[i]
            if i == 0 { continue }
            for j in 0..<i {
                if nums[i] > nums[j] {
                    cache[i] = max(cache[i], cache[j] + 1)
                }
                ans = max(ans, cache[i])
            }
        }
        return ans
    }
}

 

반응형

'PS > LeetCode' 카테고리의 다른 글

Combination Sum IV  (2) 2025.02.03
Word Break  (0) 2025.02.03
Climbing Stairs  (1) 2025.02.02
Container With Most Water  (0) 2025.01.12
3Sum  (0) 2025.01.12
'PS/LeetCode' 카테고리의 다른 글
  • Combination Sum IV
  • Word Break
  • Climbing Stairs
  • Container With Most Water
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (304)
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (37)
        • swift (32)
        • python (4)
        • Kotlin (1)
      • iOS (132)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (13)
        • 어노테이션 (1)
        • 튜토리얼 (11)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
      • AI (0)
        • 이론 (0)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    AVFoundation
    프로그래머스
    dfs
    Swift
    백준
    Tuist
    투포인터
    UIKit
    concurrency
    lifecycle
    IOS
    protocol
    dp
    CS
    Spring
    SwiftUI
    GIT
    boostcamp
    property
    dispatch
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
300. Longest Increasing Subsequence
상단으로

티스토리툴바