问题描述:

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. ARM学习笔记14——C语言和汇编相互套用

    这里,我们要准备两个文件,一个是汇编文件start.S,另一个是C文件led.c.汇编文件套用C文件中的开关灯函数,C文件套用汇编文件中延时函数. //start.S .global led_init ...

  2. Web---字节输出流和字符输出流的共存问题、转发、重定向、请求包含知识点讲解

    本章博客的知识点: 1.字节输出流和字符输出流的共存问题 2.转发时,两个servlet都输出信息的问题 详细知识,见OneServlet和TwoServlet源代码中的注释 转发:传参,访问顺序(d ...

  3. strcmp函数实现及分析

    最近看C,看到strcmp函数,对它的实现原型不很清楚,于是到网上搜.网上算法一大堆,看了很多代码后自己做了一下总结 strcmp函数是C/C++中基本的函数,它对两个字符串进行比较,然后返回比较结果 ...

  4. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  5. Bzoj 1982: [Spoj 2021]Moving Pebbles 博弈论

    1982: [Spoj 2021]Moving Pebbles Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 130  Solved: 88[Submi ...

  6. hdoj 2035 人见人爱A^B

    人见人爱A^B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. NOIP-2003 加分二叉树

    题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...

  8. Lab-Data-Systems-for-Biomanufacturing 生物制药企业实验室数据系统(Starlims)

  9. SQL基础之 时间戳

    本文转载:http://www.cnblogs.com/liuhh/archive/2011/05/14/2046544.html 一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一 ...

  10. 我的第一个 Rails 站点:极简优雅的笔记工具-Raysnote

    出于公司开发需求,这个暑假我開始搞Ruby on Rails.在业余时间捣鼓了一个在线笔记应用:http://raysnote.com.这是一个极简而优雅的笔记站点(至少我个人这么觉得的). 笔记支持 ...