파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 8. 5. 23:45

https://leetcode.com/problems/combination-sum-iv/

 

Combination Sum IV - 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를 이용한 간단한 문제

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp = [0 for _ in range(target+1)]
        dp[0] = 1
        
        for i in range(1, target+1):
            for num in nums:
                num_before = i - num
                if num_before >= 0:
                    dp[i] += dp[num_before]
        return dp[target]
반응형

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

Today's Challenge  (0) 2022.08.07
Today's Challenge  (0) 2022.08.06
Today's Challenge  (0) 2022.08.04
Today's Challenge  (0) 2022.08.03
Today's Challenge  (0) 2022.08.02
Comments