Swift solution - Dynamic Programming


  • 0
    class Solution {
        func integerBreak(_ n: Int) -> Int {
            var dp = [Int](repeatElement(0, count: n + 1))
            
            dp[1] = 1
            for i in 2...n {
                for j in 1..<i {
                    dp[i] = max(dp[i], max(j, dp[j]) * max(i - j, dp[i - j]))
                }
            }
            
            return dp[n]
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.