베어_
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)

블로그 메뉴

    공지사항

    인기 글

    태그

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

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/LeetCode

    [LeetCode] Longest Common Subsequence (Java)

    2023. 7. 16. 20:42

    ✉️문제

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

     

    Longest Common Subsequence - LeetCode

    Can you solve this real interview question? Longest Common Subsequence - Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string genera

    leetcode.com

     

    🗝 문제풀이

    2차원 배열을 사용하는 기본적인 다이나믹 프로그래밍 문제이다.

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

    class Solution {
        public int longestCommonSubsequence(String text1, String text2) {
    
            int n = text1.length();
            int m = text2.length();
            int[][] dy = new int[n + 1][m + 1];
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= m; j++) {
                    if(text1.charAt(i - 1) == text2.charAt(j - 1)) {
                        dy[i][j] = dy[i - 1][j - 1] + 1;
                    } else {
                        dy[i][j] = Math.max(dy[i - 1][j], dy[i][j - 1]);
                    }
                }
            }
            return dy[n][m];
        }
    }
    저작자표시 비영리 변경금지 (새창열림)
      '알고리즘/LeetCode' 카테고리의 다른 글
      • [LeetCode] Combination Sum IV (Java)
      • [LeetCode] Word Break (Java)
      • [LeetCode] Longest Increasing Subsequence (Java)
      • [LeetCode] Non-overlapping Intervals (Java)
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바