베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • 코드업
    • 스프링시큐리티
    • 토비스프링
    • C++
    • leetcode
    • 백준
    • 이펙티브자바
    • 스프링
    • java
    • 스레드
    • 자바8
    • 함수형인터페이스
    • BFS
    • 데이터베이스
    • Spring
    • 운영체제
    • jpa
    • 자바
    • 알고리즘
    • dfs

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Longest Increasing Subsequence (Java)

    2023. 7. 16. 19:24

    ✉️문제

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

     

    Longest Increasing Subsequence - LeetCode

    Can you solve this real interview question? Longest Increasing Subsequence - Given an integer array nums, return the length of the longest strictly increasing subsequence.   Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest

    leetcode.com

     

    📝 접근

    1. 다이나믹 프로그래밍을 이용한 방법 

     - D[i] = i번 째에서 구할 수 있는 가장 긴 수열

     - 시간 복잡도 : O(N^2)

    2. 그리디와 이진 탐색을 이용한 방법

     - 시간 복잡도 : O(n log n)

     

    🗝 문제풀이

    1. 다이나믹 프로그래밍

    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length + 1];
        Arrays.fill(dp, 1);
        for(int i = 1; i < nums.length; i++) {
    
            for(int j = 0; j <= i; j++) {
                if(nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }
    
        int res = Integer.MIN_VALUE;
        for(int n : dp) {
            res = Math.max(n, res);
        }
    
        return res;
    }

     

    2. 그리디와 이진탐색

    public int lengthOfLIS2(int[] nums) {
        ArrayList<Integer> res = new ArrayList<>();
        for(int i = 0; i < nums.length; i++) {
            if(res.isEmpty() || nums[i] > res.get(res.size() - 1)) {
                res.add(nums[i]);
            } else {
                searchRightPlace(res, nums[i]);
            }
        }
    
        return res.size();
    }
    
    private void searchRightPlace(ArrayList<Integer> res, int num) {
        int l = 0;
        int r = res.size() - 1;
        while(l < r) {
            int mid =  (l + r) / 2;
            if (num > res.get(mid)) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        res.set(l, num);
    }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Combination Sum IV (Java)
      • [LeetCode] Word Break (Java)
      • [LeetCode] Longest Common Subsequence (Java)
      • [LeetCode] Non-overlapping Intervals (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바