LeetCode - Integer Break

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.

Solution:

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

    }
}

  

时间: 2024-11-05 14:40:34

LeetCode - Integer Break的相关文章

[LeetCode][Python]Integer Break

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

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, given s = "leetcode", dict = ["leet", "code"]. Return true because

LeetCode: Word Break [139]

[题目] 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, given s = "leetcode", dict = ["leet", "code"]. Return true be

LeetCode: Word Break II [140]

[题目] 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", "cat

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/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 = "c

Leetcode | Work 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 &

[Math_Medium]343. Integer Break

原题: 343. Integer Break 题目大意:给你一个数(2-57), 将这个数拆成若干项(和不变),使其乘积最大. 解题思路: 我们知道,一个数,无论拆成多少项,当每一项都相等时,其乘积最大:假设我们将N拆成N/x个x,于是其乘积就是 x^(N/x),我们对这个乘积求导: 可知,当x=e时,倒数为0,此时函数取得最大值,而我们在这里只能取整数,所以可以取2或者3,但是我们发现 2x2x2 < 3x3,所以我们应该尽可能地取3,所以代码如下: 源代码: class Solution{

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 +

【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, re