[LeetCode][Python3] 300. Longest Increasing Subsequence
                2019. 2. 10. 02:09 |
                
                    프로그래밍/LeetCode
                
            
            
            
        Problem :
https://leetcode.com/problems/longest-increasing-subsequence/
My Solution :
class Solution:
    def lengthOfLIS(self, nums: 'List[int]') -> 'int':
        lis = nums[:1]
        for n in nums[1:]:
            low, high = 0, len(lis)
            while low < high:
                mid = (low + high) // 2
                if lis[mid] < n:
                    low = mid + 1
                else:
                    high = mid
            if low == len(lis):
                lis.append(n)
            else:
                lis[low] = n
        return len(lis)
Comment :
LIS(Longest Increasing Subsequence)는 가장 전형적인 DP 문제이다. 위 접근법은 실제로 LIS를 만들어놓고 이진 탐색으로 삽입 위치를 찾는다. 삽입될 index에 기존 값이 있다면 n은 그 값보다 작거나 같기 때문에 갱신하고, 삽입될 index가 LIS 길이와 같다면, 즉 LIS의 최대값(마지막 원소)보다 n이 크다면 append 한다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 128. Longest Consecutive Sequence (0) | 2019.03.23 | 
|---|---|
| [LeetCode][Python3] 42. Trapping Rain Water (0) | 2019.03.23 | 
| [LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 | 
| [LeetCode][Python3] 200. Number of Islands (0) | 2019.02.11 | 
| [LeetCode][Python3] 240. Search a 2D Matrix II (0) | 2019.02.09 | 
| [LeetCode][Python3] 103. Binary Tree Zigzag Level Order Traversal (0) | 2019.02.07 | 
| [LeetCode][Python3] 279. Perfect Squares (0) | 2019.01.31 | 
| [LeetCode][Python3] 162. Find Peak Element (0) | 2019.01.25 | 
최근에 달린 댓글 최근에 달린 댓글