【Leetcode】Integer Break

题目链接:https://leetcode.com/problems/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.

思路:

找规律,n>=7时,c[i]=c[i-3]*3

算法

public int integerBreak(int n) {
    int res6[] = new int[] { 0, 0, 1, 2, 4, 6, 9 };
    if (n <= 6)
        return res6[n];  

    int res[] = new int[n + 1];
    for (int i = 2; i <= 6; i++) {
        res[i] = res6[i];
    }
    for (int i = 7; i <= n; i++) {
        res[i] = res[i - 3] * 3;
    }
    return res[n];
}
时间: 2024-10-14 00:36:57

【Leetcode】Integer Break的相关文章

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Integer to Roman 和 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [罗马数字] 1~9: {"I", "II", "III", "IV", "V"

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【leetcode】Word Break (middle)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =

【leetcode】Word Break(python)

思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为