• 0730


    给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。

     

    动态规划

    ================================Python=================================

    class Solution:
        def integerBreak(self, n: int) -> int:
            dp = [0 for i in range(n+1)]
            for i in range(1, n+1):
                for j in range(i):
                    dp[i] = max(dp[i], dp[i-j] * j, j * (i - j))
            return dp[-1]

    ================================Java==================================

    class Solution {
        public int integerBreak(int n) {
            int[] dp = new int[n+1];
            for (int i = 2; i <= n; i ++){
                int curMax = 0;
                for (int j = 1; j <= i - 1; j++){
                    curMax = Math.max(curMax, Math.max(dp[i - j] * j, (i - j) * j));
                }
                dp[i] = curMax;
            }
            return dp[n];
        }
    }
    class Solution {
        public int integerBreak(int n) {
            int[] dp = new int[n+1];
            for (int i = 1; i <= n; i ++){
                for (int j = 0; j <= i - 1; j++){
                    dp[i] = Math.max(dp[i], Math.max(dp[i - j] * j, (i - j) * j));
                }
            }
            return dp[n];
        }
    }

    =============================Go=============================

    func integerBreak(n int) int {
        dp := make([]int, n+1)
        for i := 2; i < n + 1; i++ {
            for j := 1; j < i; j++ {
                dp[i] = max(dp[i], max(dp[i - j] * j, (i - j) * j))
            }
        }
        return dp[n]
    }
    
    func max(m, n int) int {
        if (m > n) {
            return m
        } else {
            return n
        }
    }
  • 相关阅读:
    Spoj-ANTP Mr. Ant & His Problem
    Spoj-BIPCSMR16 Team Building
    cf575A Fibonotci
    cf660E Different Subsets For All Tuples
    cf468C Hack it!
    cf468B Two Sets
    cf701E Connecting Universities
    Spoj-BLMIRINA Archery Training
    Spoj-BITDIFF Bit Difference
    Spoj-VISIBLEBOX Decreasing Number of Visible Box
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13402154.html
Copyright © 2020-2023  润新知