파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 17. 23:45

https://leetcode.com/problems/k-inverse-pairs-array/

 

K Inverse Pairs Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

계속 DP 관련 문제가 나옴.
DP는 참.... 풀기가 어렵네 ㅠㅠ

class Solution:
    def kInversePairs(self, n: int, k: int) -> int:
        m = 10**9 + 7
        
        dp = [0] * (k+1)
        dp[0] = 1
        
        for i in range(n):
            ddp = []
            s = 0            
            for j in range(k+1):
                s += dp[j]
                if j >= i + 1:
                    s -= dp[j - i - 1]
                s %= m
                ddp.append(s)
            dp = ddp
        return dp[-1]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.07.19
Today's Challenge  (0) 2022.07.18
Today's Challenge  (0) 2022.07.16
Today's Challenge  (0) 2022.07.15
Today's Challenge  (0) 2022.07.14
Comments