成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Integer Break

leonardofed / 994人閱讀

Problem

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

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

Solution

1. beat 40.83% O(log(n))

public class Solution {
    public int integerBreak(int n) {
        if (n < 4) return n-1;
        else if(n%3 == 0)
            return (int)Math.pow(3, n/3);
        else if(n%3 == 1)
            return 4*(int)Math.pow(3, (n-4)/3);
        else 
            return 2*(int)Math.pow(3, n/3);
    }
}

2. beat 40.83% O(n)

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

3. DP beat 40.83% O(n)

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66206.html

相關(guān)文章

  • LeetCode 343. Integer Break

    摘要:思路動(dòng)態(tài)規(guī)劃,前五個(gè)數(shù)的最大乘積為,后面的第個(gè)數(shù)的最大乘積,由從后往前數(shù)包括本身的第三個(gè)數(shù)乘以得到。何睿何睿前個(gè)數(shù)的最大乘積動(dòng)態(tài)規(guī)劃第個(gè)數(shù)的最大乘積為往前數(shù)第三個(gè)數(shù)思路與上面的思路一致,優(yōu)化了空間源代碼文件在這里。 Description Given a positive integer n, break it into the sum of at least two positive...

    ckllj 評(píng)論0 收藏0
  • leetcode 343. Integer Break

    摘要:題目要求將一個(gè)正整數(shù)分解為兩個(gè)或兩個(gè)以上的正整數(shù),要求這些正整數(shù)的乘積最大。思路和代碼這里應(yīng)用了一個(gè)數(shù)學(xué)的思路。假設(shè)我們有一個(gè)數(shù)字,該數(shù)組可以隨機(jī)分解為和。因此取時(shí)可以得到最好的結(jié)果。至于為什么我們需要盡可能用分解,因?yàn)椤? 題目要求 Given a positive integer n, break it into the sum of at least two positive in...

    233jl 評(píng)論0 收藏0
  • [LeetCode] 811. Subdomain Visit Count

    Problem A website domain like discuss.leetcode.com consists of various subdomains. At the top level, we have com, at the next level, we have leetcode.com, and at the lowest level, discuss.leetcode.com...

    jzman 評(píng)論0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 計(jì)算逆波蘭表

    摘要:棧法復(fù)雜度時(shí)間空間思路逆波蘭表達(dá)式的計(jì)算十分方便,對(duì)于運(yùn)算符,其運(yùn)算的兩個(gè)數(shù)就是這個(gè)運(yùn)算符前面的兩個(gè)數(shù)。注意對(duì)于減法,先彈出的是減號(hào)后面的數(shù)。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...

    ephererid 評(píng)論0 收藏0
  • [LeetCode] 8. String to Integer (atoi)

    Problem Implement function atoi to convert a string to an integer. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values...

    cuieney 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<