• leetcode 343. Integer Break(dp或数学推导)


    Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

    For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

    Note: you may assume that n is not less than 2.

    Hint:

    1. There is a simple O(n) solution to this problem.
    2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.

    题意:给一个n(n>=2),求相加等于n且乘积最大的一组整数的积。

    题解

    这其实就是一道高中数学题,但是leetcode上的数据比较水,导致完全不用数学推导的O(n2)的dp也可以过。

    解法一(纯dp):

    令dp[n]为n对应的最大积。

    那么递推方程就是:dp[n]=max(i*dp[n-i],i*(n-i))(其中i从1到n-1)。

    边界:dp[2]=1;

    时间复杂度:O(n2)

    class Solution {
    public:
        int integerBreak(int n) {
            int dp[n];
            dp[1]=1;
            dp[2]=1;
    for(int i=3;i<=n;i++){
                dp[i]=-1;
                for(int j=1;j<i;j++){
                    dp[i]=max(j*dp[i-j],max(dp[i],j*(i-j)));
                }
            }
            return dp[n];
        }
    };

    解法二(当成数学题来做就好):

    由均值不等式(n个数的算术平均数大于等于它们的几何平均数):

    mathbf{A}_n =  frac{x_1 + x_2 + cdots + x_n}{n}    >=   mathbf{G}_n = sqrt[n]{x_1 cdot x_2 cdots x_n}

    得:当把输入的n拆分成几个相等的数时它们的积最大。

    那么问题来了,拆分成几个呢?

    为了方便使用导数,我们先假设我们可以把n拆分成实数。那么设每一个数为x,则一共有n/x个数。

    设它们的积为f(x),则f(x)=x(n/x),那么怎么求f(x)最大值呢?求导数!

    f′(x)=(n/x2)  *  x(n/x)  * (1-lnx)

    当x=e时取极大值。

    而我们题目里规定x为整数,那么我们只需要取的x越靠近e越好。那么2<e<3,而且e=2.71828...,所以取3是最好的,如果取不到3就取2。

    幂运算复杂度为O(lgn),所以这个算法复杂度为O(lgn)。

    class Solution {
    public:
        int integerBreak(int n) {
            if(n == 2)
                return 1;
            else if(n == 3)
                return 2;
            else if(n%3 == 0)
                return pow(3, n/3);
            else if(n%3 == 1)
                return 2 * 2 * pow(3, (n - 4) / 3);
            else 
                return 2 * pow(3, n/3);
        }
    };
  • 相关阅读:
    树的直径证明+HDU2196
    区间DP的摸索
    HDU1502 Regular Words DP+大数
    HDU1501 dfs
    “龙井”开箱评测 |Alibaba Dragonwell 新手上路指南
    只想着一直调用一直爽, 那API凭证泄漏风险如何破?
    使用Data Lake Analytics读/写RDS数据
    使用Data Lake Analytics从OSS清洗数据到AnalyticDB
    阿里巴巴资深技术专家无相:我们能从 InteliJ IDEA 中学到什么?
    云数据库RDS存储能力进化解析!
  • 原文地址:https://www.cnblogs.com/zywscq/p/5415303.html
Copyright © 2020-2023  润新知