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