[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 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.

https://leetcode.com/problems/integer-break/



把一个数字拆成几个数字相加,这几个数的乘积要最大。

先是O(n^2)的动态规划。某个数最大的乘积,总是由比它小的数的子结果,乘以他们之间的差得来的。

但是注意也有可能是拆成两个数直接相乘的结果(从后面推断可以知道是小于6的数)。

 1 class Solution(object):
 2     def integerBreak(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         dp = [0, 1, 1]
 8         for i in range(3, n + 1):
 9             maxInt = -1
10             for j in range(1, i):
11                 maxInt = max(maxInt, (i - j) * dp[j], (i - j) * j)
12             dp.append(maxInt)
13         return dp[n]

然后找规律,从大于7的数开始,符合规律。

为什么要以3为单位,不是4也不是5,因为:

3 * 3 * 3 * 3 > 4 * 4 * 4
3 * 3 * 3 * 3 * 3 > 5 * 5 * 5

https://leetcode.com/discuss/98249/easy-to-understand-c-with-explanation

 1 class Solution(object):
 2     def integerBreak(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         dp = [0, 1, 1, 2, 4, 6, 9]
 8         if n <= 6:
 9             return dp[n]
10         return 3 * self.integerBreak(n - 3)
时间: 2024-10-10 21:00:46

[LeetCode][Python]Integer Break的相关文章

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

[LeetCode][Python]Integer to Roman

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/integer-to-roman/Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.===Comments by Daba

LeetCode 343. Integer Break

https://leetcode.com/problems/integer-break/ 数学题. 1 #include <iostream> 2 using namespace std; 3 4 class Solution { 5 public: 6 int integerBreak(int n) { 7 if (n == 2) return 1; 8 if (n == 3) return 2; 9 10 int product = 1; 11 while (n > 4) 12 {

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

LeetCode:Integer to English Words - 按英语读法输出数字对应单词

1.题目名称 Integer to English Words(按英语读法输出数字对应单词) 2.题目地址 https://leetcode.com/problems/integer-to-english-words/ 3.题目内容 英文:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 中文:给出一个非负整数

Python integer objects implementation

http://www.laurentluce.com/posts/python-integer-objects-implementation/ Python integer objects implementation May 15, 2011 This article describes how integer objects are managed by Python internally. An integer object in Python is represented interna

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上