일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- FIT XR
- 잡생각
- English
- Problem Solving
- 뭐든
- Writing
- leetcode
- 개발자
- 10분
- 매일
- 사이드
- 프로젝트
- 3줄정리
- 파비최
- 리얼 클래스
- 영어공부
- 미드시청
- 링피트
- Daily Challenge
- 30분
- 쓰릴오브파이트
- realclass
- 읽기
- 월간
- 괜찮음
- 스탭퍼
- 운동
- 영어원서읽기
- 화상영어
- 만화도
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.06.17 Today's Challenge 본문
https://leetcode.com/problems/make-array-strictly-increasing/
Make Array Strictly Increasing - LeetCode
Can you solve this real interview question? Make Array Strictly Increasing - Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing. In one operation, you can choose two ind
leetcode.com
DP 문제
class Solution:
def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int:
dp = {}
arr2.sort()
def dfs(i, prev):
if i == len(arr1):
return 0
if (i, prev) in dp:
return dp[(i, prev)]
cost = float('inf')
if arr1[i] > prev:
cost = dfs(i+1, arr1[i])
idx = bisect.bisect_right(arr2, prev)
if idx < len(arr2):
cost = min(cost, 1+dfs(i+1, arr2[idx]))
dp[(i,prev)] = cost
return cost
res = dfs(0, -1)
return res if res < float('inf') else -1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.06.19 Today's Challenge (0) | 2023.06.19 |
---|---|
2023.06.18 Today's Challenge (0) | 2023.06.18 |
2023.06.16 Today's Challenge (0) | 2023.06.16 |
2023.06.15 Today's Challenge (0) | 2023.06.15 |
2023.06.14 Today's Challenge (0) | 2023.06.14 |
Comments