Problem Solving/LeetCode

2023.10.06 Today's Challenge

fabichoi 2023. 10. 6. 23:45

https://leetcode.com/problems/integer-break/

 

LeetCode - The World's Leading Online Programming Learning Platform

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 integerBreak(self, n: int) -> int:
        @cache
        def dp(num):
            if num <= 3:
                return num
            
            ans = num

            for i in range(2, num):
                ans = max(ans, i * dp(num-i))
            
            return ans

        if n <= 3:
            return n - 1
        
        return dp(n)
반응형