일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 매일
- Writing
- 화상영어
- FIT XR
- 파비최
- 3줄정리
- leetcode
- 읽기
- Daily Challenge
- 프로젝트
- 월간
- realclass
- 개발자
- 만화도
- 쓰릴오브파이트
- 미드시청
- 잡생각
- 뭐든
- 영어원서읽기
- 운동
- English
- 30분
- 사이드
- 영어공부
- 10분
- Problem Solving
- 링피트
- 스탭퍼
- 리얼 클래스
- 괜찮음
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.11 Today's Challenge 본문
https://leetcode.com/problems/uncrossed-lines/
Uncrossed Lines - LeetCode
Can you solve this real interview question? Uncrossed Lines - You are given two integer arrays nums1 and nums2. We write the integers of nums1 and nums2 (in the order they are given) on two separate horizontal lines. We may draw connecting lines: a straigh
leetcode.com
단순 한 방향으로 진행하는게 아니라, 양방향으로 봐야되는 케이스가 존재함.
그래서 DP로 풀어야 됨.
class Solution:
def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
n1, n2 = len(nums1), len(nums2)
memo = {}
def solve(i, j):
if i <= 0 or j <= 0:
return 0
if (i, j) in memo:
return memo[(i, j)]
if nums1[i-1] == nums2[j-1]:
memo[(i,j)] = 1 + solve(i-1, j-1)
else:
memo[(i,j)] = max(solve(i-1, j), solve(i, j-1))
return memo[(i, j)]
return solve(n1, n2)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.13 Today's Challenge (1) | 2023.05.13 |
---|---|
2023.05.12 Today's Challenge (0) | 2023.05.12 |
2023.05.10 Today's Challenge (0) | 2023.05.10 |
2023.05.09 Today's Challenge (0) | 2023.05.09 |
2023.05.08 Today's Challenge (0) | 2023.05.08 |
Comments