343. Integer Break -- Avota

时间:2022-05-26 07:48:17

问题描述:

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(其实后来测试发现n到了59其结果就超出int的表示范围了),将其拆解成至少两个正整数的和,计算拆解数的乘积,找出所能获得的最大乘积。这种输入某个数求取某种最好解的题目最简单有效的方法就是多列几个例子看看是否有规律,对于这题我们先列出4到12的结果看看:

整数n 乘积最大对应的拆解(可能有多种) 最大乘积 模3
4 2 * 2 4 1
5 3 * 2 3*2 2
6 3 * 3 3^2 0
7 3 * 2 * 2 3*4 1
8 3 * 3 * 2 3^2*2 2
9 3 * 3 * 3 3^3 0
10 3 * 3 * 2 * 2 3^2*4 1
11 3 * 3 * 3 * 2 3^3*2 2
12 3 * 3 * 3 * 3 3^4 0

仔细观察拆解结果与n模3对应关系,发现余数为0时最大乘积为3的n/3次幂,余数为1时最大乘积为4乘以3的(n/3-1)次幂,余数为2时最大乘积为2乘以3的n/3次幂。

示例代码:

class Solution {
public:
int integerBreak(int n) {
if(n == 2){
return 1;
}
if(n == 3){
return 2;
} int k = n / 3;
int yu = n - k * 3;
int result = 0; if(yu == 1){
result = 4 * pow(3, k-1);
}
else if(yu == 2){
result = 2 * pow(3, k);
}
else{
result = pow(3, k);
}
return result;
}
};

343. Integer Break -- Avota的更多相关文章

  1. LN : leetcode 343 Integer Break

    lc 343 Integer Break 343 Integer Break Given a positive integer n, break it into the sum of at least ...

  2. #Week 11 - 343.Integer Break

    Week 11 - 343.Integer Break Given a positive integer n, break it into the sum of at least two positi ...

  3. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  4. [LeetCode] 343. Integer Break 整数拆分

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

  5. (dp)343. Integer Break

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

  6. Leetcode 343. Integer Break

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

  7. 343. Integer Break

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

  8. leetcode@ [343] Integer Break (Math & Dynamic Programming)

    https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...

  9. LeetCode题解 343.Integer Break

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

随机推荐

  1. 18.1---不用加号的加法(CC150)

    1,自己写的又长又臭的代码,也能AC,但是太丑了.主要是通过二进制来算. public static int addAB(int a, int b){ int res = 0; String str1 ...

  2. HtmlEncode、HtmlDecode、UrlEncode、UrlDecode

    HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码.例如:"<".">"."&" 等. HtmlDe ...

  3. C&plus;&plus;拾遗

    1三个概念 字符串字面值是一串常量字符(是一个常量),字符串字面值常量用双引号括起来的零个或多个字符表示,为兼容C语言,C++中所有的字符串字面值都由编译器自动在末尾添加一个空字符.字符串字面值的类型 ...

  4. mysql初始化提示安装perl

    all_db --user=mysql --datadir=/data/mysql", "delta": "0:00:00.222500", &quo ...

  5. 4&period;3Python数据类型(3)之字符串类型

    返回总目录 目录: 1.字符串的概念 2.字符串的形式 3.字符串的转义符 4.字符串一般操作 5.字符串函数操作 (一)字符串的概念 由单个字符组成的一个集合 (二)字符串的形式 双引号与单引号的效 ...

  6. Java中的枚举Enum

    public class TestEnum { /*最普通的枚举*/ public enum ColorSelect { red, green, yellow, blue; } /* 枚举也可以象一般 ...

  7. Python&plus;OpenCV图像处理(五)—— 像素运算

    最近在忙毕业设计,只能偶尔更新博客........ 一.像素的算术运算 像素的算术运算涉及加减乘除等基本运算(要进行算术运算,两张图片的形状(shape)必须一样) 代码如下: #像素的算术运算(加. ...

  8. Sublime Text 格式化JSON-pretty json

    1.安装install package 按control + `,打开命令输入框 输入一下命令: import urllib2,os; pf='Package Control.sublime-pack ...

  9. 自我介绍及如何注册GITHUB

    自我介绍 我是来自南通大学网络工程141班的周楠,我的学号是1413042014,我的兴趣是喜欢玩游戏(如果这算是一个兴趣爱好的话),喜欢尝试各种游戏. 如何注册一个GitHub账号? 1.首先我们需 ...

  10. SNP芯片的原理

    Illumina的SNP芯片原理 Illumina的SNP生物芯片的优势在于: 第1,它的检测通量很大,一次可以检测几十万到几百万个SNP位点 第2,它的检测准确性很高,它的准确性可以达到99.9%以 ...