saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 파이썬
  • c++
  • 취준
  • 온라인저지
  • DFS
  • 알고리즘문제해결
  • BFS
  • LeetCode
  • 알고리즘
  • Python
  • 개발자
  • 백준
  • 리트코드
  • 취업준비
  • 딕셔너리
  • 딥러닝
  • 릿코드
  • two pointer
  • 개발자 취업준비
  • 문제해결능력

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 300. Longest Increasing Subsequence

2022. 8. 10. 14:27

300. Longest Increasing Subsequence

Medium

Given an integer array nums, return the length of the longest strictly increasing subsequence.

A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].

 

Example 1:

Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Example 2:

Input: nums = [0,1,0,3,2,3]
Output: 4

Example 3:

Input: nums = [7,7,7,7,7,7,7]
Output: 1

 

Constraints:

  • 1 <= nums.length <= 2500
  • -104 <= nums[i] <= 104

문제 풀이

문제 접근

  1. 제한 조건이 10^4 * 2 이므로 시간복잡도는 N^2 까지 가능해보인다. 
  2. 모든 값을 확인하면서 만들 수 있는 배열을 생성해야한다.
  3. 주어진 리스트를 차례로 지나치면서, 기존의 추가된 정답 리스트의 값을 교체해준다.

풀이

  1. 리스트의 처음 값을 정답 리스트에 추가한다.
  2. 그 이후 값이 정답 리스트의 마지막 값보다 크다면 정답 리스트 마지막에 넣는다.
  3. 반대로 그 값이 정답 리스트의 마지막 값보다 작다면 앞의 모든 숫자를 확인해봐야한다.
  4. 정답 리스트의 앞쪽 숫자가 현재 숫자보다 작으면 그 자리의 숫자와 교체 해준다.
    1. 중간에 작은 숫자들을 교체해줘야 하는 이유는 새로운 숫자들의 집합이 이미 구해진 정답 집합보다 작을 수 있기 때문이다.
    2. 예시: [3,5,6,2,5,4,19,5,6,7,12]
      1. 숫자를 마지막에만 바꿨을때 정답: 3, 5, 6, 7, 12
      2. 앞의 모든 작은 수를 교체했을때 정답: 2, 4, 5, 6, 7, 12 // (3, 5, 6) -> (2, 5, 6) -> (2, 4, 6) -> (2, 4, 6, 19) -> (2, 4, 5, 19) -> (2, 4, 5, 6) ... (2, 4, 5, 6, 7, 12)
      3. (2, 4, 5, 19) 부분에서 앞의 숫자들이 모두 작아지는 것으로 교체가 됬기 때문에 19와 6을 교체할 수 있다.
  5. 만들어진 정답 리스트의 길이를 리턴한다. 

소스코드

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        sub = [nums[0]]
        for num in nums[1:]:
            if num > sub[-1]:
                sub.append(num)
            else:
                idx = 0
                while num > sub[idx]:
                    idx += 1
                sub[idx] = num
        return len(sub)
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 314. Binary Tree Vertical Order Traversal  (0) 2022.08.11
[LeetCode] 98. Validate Binary Search Tree  (0) 2022.08.11
[LeetCode] 108. Convert Sorted Array to Binary Search Tree  (0) 2022.08.10
[LeetCode] 621. Task Scheduler  (0) 2022.07.30
[LeetCode] 916. Word Subsets  (0) 2022.07.30
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 314. Binary Tree Vertical Order Traversal
    • [LeetCode] 98. Validate Binary Search Tree
    • [LeetCode] 108. Convert Sorted Array to Binary Search Tree
    • [LeetCode] 621. Task Scheduler
    saurus2
    saurus2
    Simple is Best

    티스토리툴바