파비의 매일매일 공부기록

2023.06.17 Today's Challenge 본문

Problem Solving/LeetCode

2023.06.17 Today's Challenge

fabichoi 2023. 6. 17. 23:45

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