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