| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 미드시청
- 잡생각
- 매일
- 뭐든
- 리얼 클래스
- 영어원서읽기
- leetcode
- Daily Challenge
- 파비최
- 개발자
- 링피트
- 만화도
- 30분
- 괜찮음
- 월간
- realclass
- 운동
- 3줄정리
- FIT XR
- 영어공부
- Writing
- 화상영어
- 프로젝트
- 읽기
- 스탭퍼
- 사이드
- Problem Solving
- 쓰릴오브파이트
- English
- 10분
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/longest-common-subsequence/
Longest Common Subsequence - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
전형적인 LCS 문제.
내부 함수를 써서 푼 솔루션을 참고
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
def lcs(x, y, m, n, visited):
k = (m, n)
if m == 0 or n == 0:
return 0
if k not in visited:
if x[m-1] == y[n-1]:
visited[k] = lcs(x, y, m-1, n-1, visited) + 1
else:
a = lcs(x, y, m-1, n, visited)
b = lcs(x, y, m, n-1, visited)
visited[k] = max(a, b)
return visited[k]
x = len(text1)
y = len(text2)
visited = {}
return lcs(text1, text2, x, y, visited)반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| Today's Challenge (0) | 2022.12.17 |
|---|---|
| Today's Challenge (0) | 2022.12.16 |
| Today's Challenge (0) | 2022.12.14 |
| Today's Challenge (0) | 2022.12.13 |
| Today's Challenge (0) | 2022.12.12 |
Comments