Post

leetcode(리트코드)1143-Common Subsequence

leetcode 1143 - Common Subsequence 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

Medium 난이도 문제입니다.
https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU에서 추천한 문제입니다.


4. 문제 해석

  • DP문제입니다.
  • 문자열 2개에서 가장 긴 공통된 문자열을 찾아 그 길이를 리턴합니다.

5. code

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        //DP테이블
        int** check;
        // row는 text1의 길이보다 1만큼 증가
        int row = text1.size()+1;
        // col도 마찬가지. 이유는 초기값을 0으로 셋팅하기 위해서
        int col = text2.size()+1;
        check=new int*[row * sizeof(int)];
        for(int i =0;i<row;++i)
            check[i] = new int[col * sizeof(int)];
        for(int i = 0;i<row;++i)
        {
            for(int j = 0 ;j<col;++j)
            {
                //초기값 세팅
                if(i==0 || j==0 )
                {
                    check[i][j] = 0;
                    continue;
                }
                //점화식 부분.
                if(text1[i-1] ==text2[j-1])
                {
                    check[i][j]= check[i-1][j-1]+1;
                }
                else
                    check[i][j] = max(check[i-1][j],check[i][j-1]);
            }
        }
        return check[row-1][col-1];
            
    }
};

6. 결과 및 후기, 개선점

코드에 대한 설명이 필요하신 분은 댓글을 달아주세요.!!

c++ 74% python ??%

This post is licensed under CC BY 4.0 by the author.