일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- English
- 사이드
- 프로젝트
- 괜찮음
- 만화도
- 운동
- Daily Challenge
- 화상영어
- 미드시청
- 월간
- 쓰릴오브파이트
- 리얼 클래스
- Writing
- 30분
- Problem Solving
- 스탭퍼
- 개발자
- FIT XR
- realclass
- 잡생각
- 읽기
- 3줄정리
- leetcode
- 10분
- 링피트
- 매일
- 뭐든
- 영어원서읽기
- 파비최
- 영어공부
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