1. 程式人生 > >LeetCode 343. Integer Break(整數分拆)

LeetCode 343. Integer Break(整數分拆)

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種拆分方法。

public class Solution {
    public int integerBreak(int n) {
        int max = 1;
        for(int i=2; i<n; i++) {
            int product = 1;
            int m=n/i, remain = n-n/i*i;
            for(int j=0; j<i; j++) {
                if (remain == 0) {
                    product *= m;
                } else {
                    product *= m + 1;
                    remain --;
                }
            }
            if (product < max) break;
            max = product;
        }
        return max;
    }
}

方法二:深度優先搜尋。

public class Solution {
    private int max = 0;
    private void find(int from, int n, int product, int step) {
        if (n==0) {
            if (step>1) max = Math.max(max, product);
            return;
        }
        for(int i=from; i<=3 && i<=n; i++) {
            find(i, n-i, product*i, step+1);
        }
    }
    public int integerBreak(int n) {
        find(1, n, 1, 0);
        return max;
    }
}

方法三:分析規律。


public class Solution {
    public int integerBreak(int n) {
        if (n<2) return 0;
        if (n==2) return 1;
        if (n==3) return 2;
        int p = 1;
        while (n>4) {
            p *= 3;
            n -= 3;
        }
        p *= n;
        return p;
    }
}

參見LeetCode的討論區:https://leetcode.com/discuss/questions/oj/integer-break?sort=votes

相關推薦

LeetCode 343. Integer Break整數

Given a positive integer n, break it into the sum of at least two positive integers and maximize

LeetCode-343 Integer Break整數拆分乘積最大

作為一道程式設計題,這道題還是很簡單的。簡單的觀察就能知道拆出足夠多的 3 就能使得乘積最大。int integerBreak(int n) { if(n == 2) return 1; if(n == 3) return 2; int ret = 1

leetcode 343.Integer Break 整數分割(c++)

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

Leetcode 343 Integer Break

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

LeetCode 343. Integer Break

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

Leetcode 343. Integer Break[Medium]

原題地址 題目內容 題目分析 題目的意思為,給定一個常數n,求出一個數字集合{Xi},使得X0+X1+…+Xi = n;並且使得X0*X1*….*Xi的結果最大 採用dp演算法。令dp[n]為最大的product。 那麼遞推方程為: dp[n]=max(i*dp[n-

LeetCode 343.Integer Break題解

題目 題目連結 隨機給你一個大於2的正整數,讓你將其拆分(這意味這至少將其拆分為兩個數),比如10可以拆分為3+3+4。最後你需要得出一種拆分方式,使得所有數之積最大。10的結果為3*3*4=36。

LeetCode343. Integer Break

Description: Given a positive integer n, break it into the sum of at least two positive integers a

leetcode】Word Breakpython

條件 text for -m 是我 tex eas sso false 思路是這種。我們從第一個字符開始向後依次找,直到找到一個斷句的地方,使得當前獲得的子串在dict中,若找到最後都沒找到。那麽就是False了。 在找到第一個後,接下來找下一個斷句處,當然是從第

【POJ 1716】Integer Intervals約束系統

入門題 put AD edge ota 全部 lib 最小 最短 id=1716">【POJ 1716】Integer Intervals(差分約束系統) In

[Math_Medium]343. Integer Break

.com alt load 題目 ans edi href 可能 script 原題: 343. Integer Break 題目大意:給你一個數(2-57), 將這個數拆成若幹項(和不變),使其乘積最大。 解題思路: 我們知道,一個數,無論拆成多少項,當每一項都相等時,其

PAT 1103 Integer Factorization 30 深度有限搜尋,關於執行超時問題 燚

The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive

leetcodeinteger to roman 整數向羅馬字元的轉換

 題目描述: 羅馬字元真是這樣轉換的嗎,太可怕了。由基本的1000,500,100,50,10,5,1組成,然後 IV(1,5)就表示4,XL(10,50)就表示40,把所有的這種組成排列出來啊,能想到的太厲害了吧,下面是轉載得到的一個比較厲害的貪心演算法,beat

leetcode-12:Integer to Roman整數轉羅馬數字

題目: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol

[LeetCode]12. Integer to Roman整數轉羅馬數字

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X

1103 Integer Factorization30

1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive i

12. Integer to Roman整數轉羅馬數字

給定一個整數,將其轉為羅馬數字。 輸入保證在 1 到 3999 之間。 解題思路:每一個不同權值的數字都有相應的羅馬數字與之對應,建立一個數組,然後用除運算和模運算得到不同權值的數字並對應輸出即可。

Leetcode#8. String to Integer (atoi)字串轉數字

宣告:題目解法使用c++和Python兩種,重點側重在於解題思路和如何將c++程式碼轉換為python程式碼。 題目 Implement atoi to convert a string to an integer. Hint: Carefully con

Leetcode 139 Word BreakDP

解題思路:dp[i]表示到第i個字元,可否被表示,每次在dp[i]=true的位置,向後列舉。class Solution { public: bool wordBreak(string s, v

HDOJ題目3440 House Man約束

etop log help crazy code sizeof inpu field empty House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/