343. Integer Break -- Avota
问题描述:
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的更多相关文章
- 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 ...
- #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 ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- [LeetCode] 343. Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- (dp)343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- Leetcode 343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- 343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- 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 ...
- LeetCode题解 343.Integer Break
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...
随机推荐
- 嵌入式linux平台搭建
选用Ubuntu12.04.2系统搭建平台.在原始系统下做如下更改: 将更新使用的服务器设置为国内“163”服务器 安装SSH,uboot—mkimage等软件 安装编译器“arm—2009q3”及相 ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- PC-[WIN7]此计算机无法联接家庭组
不能启用 HomeGroup Listener 解决: 1.设定为自动开始:Function Discovery Provider HostFunction Discovery Resource Pu ...
- 中文字符串的编码转换(c实现)
中文字符串在c/c++中表示为字节序列,在分词的时候需要根据不同的编码方式进行分词,一般分词器需要转换成统一的编码方式再进行转换,有些分词器如ICTCLAS在分词的时候可以不显示定义编码方式,可以检测 ...
- 使用GruntJS链接与压缩多个JavaScript文件
使用GruntJS链接与压缩多个JavaScript文件 自己写了个简单的HTML5 Canvas的图表库,可以支持饼图,折线图,散点图,盒子图 柱状图,同时支持鼠标提示,绘制过程动画效果等.最终我想 ...
- Redis未授权访问缺陷让服务器沦为肉鸡
朋友的一个项目说接到阿里云的告警,提示服务器已沦为肉鸡,网络带宽被大量占用,网站访问很慢,通过SSH远程管理服务器还频繁断开链接.朋友不知如何下手,便邀请我帮忙处理. 阿里云的安全告警邮件内容: 在没 ...
- storm spout的速度抑制问题
转发请注明原文地址:http://www.cnblogs.com/dongxiao-yang/p/6031398.html 最近协助同事优化一个并发消费kafka数据用来计算的任务,压测过程中发现有两 ...
- ABAP(笔记)
1.excel表格上传 *&---------------------------------------------------------------------* ** 程序名称:ZSD ...
- Windows下安装Eric5时出现的“Sorry, please install QtHelp.”问题解决办法
解决Windows下安装Eric5时出现的“Sorry, please install QtHelp.”问题 PyQt4在Windows中使用了DirectX作为加速,不过,PyQt4没有使用最新 ...
- UDP 校检和和算法
#include <Winsock2.h> #include <stdio.h> #define IP_HDRINCL 2 // Header is included with ...