343.Integer Break---dp
题目链接:https://leetcode.com/problems/integer-break/description/
题目大意:给定一个自然数,将其分解,对其分解的数作乘积,找出最大的乘积结果。例子如下:
法一(借鉴):dp,一维dp,dp[i]表示i的最大分解结果的乘积,而dp[5]可以dp[4],dp[3],dp[2],dp[1]为基础,dp[4]可以dp[3],dp[2],dp[1]为基础,代码如下(耗时1ms):
//dp[i]表示给定i分解后能得到的最大乘积值
public int integerBreak(int n) {
int dp[] = new int[n + 1];
dp[1] = 1;
dp[2] = 1;
for(int i = 3; i <= n; i++) {
for(int j = 1; j <= i / 2; j++) {
//max里面要加上dp[i],因为里层for循环会不断更新dp[i],否则dp[i]得到就是最后一次的计算结果,而取不到最大值
//后面Math.max(j, dp[j]) * Math.max(i - j, dp[i - j]),因为j+(i-j)=i,所以计算j和i-j的乘积,是正常的,只不过这里可以用到先前已经算过的dp[j]和dp[i-j],因为dp[j]的结果就是j的最大分解结果,那么也可以是i的分解结果
dp[i] = Math.max(dp[i], Math.max(j, dp[j]) * Math.max(i - j, dp[i - j]));
}
}
return dp[n];
}
法二(借鉴):数学方法,数学原理:https://leetcode.com/problems/integer-break/discuss/80721/Why-factor-2-or-3-The-math-behind-this-problem.,由数学知,一个整数分解,当分解成相同的数时,乘积最大,而由于给定的自然数不一定都能分解成相同的数,所以又由数学知,求导办法见https://www.cnblogs.com/zywscq/p/5415303.html,当分解得到的数越靠近e,得到的乘积值越大,那么也就是能取3则取3,不能则取2。而又如6=2+2+2=3+3,又2*2*2<3*3,所以当能分解成3个2时,应该换算成2个3,所以下面与3求余,然后分情况分解。代码如下(耗时0):
public int integerBreak(int n) {
if(n == 2) {
return 1;
}
else if(n == 3) {
return 2;
}
else if(n == 1) {
return 1;
}
else if(n % 3 == 0) {
return (int)Math.pow(3, n / 3);
}
else if(n % 3 == 1) {
return 2 * 2 * (int)Math.pow(3, (n - 4) / 3);
}
else {
return 2 * (int)Math.pow(3, (n - 2) / 3);
}
}
343.Integer Break---dp的更多相关文章
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- 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:// ...
- (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 ...
- 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 ...
随机推荐
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- BZOJ4735 你的生命已如风中残烛 【数学】
题目链接 BZOJ4735 题解 给定一个序列,有的位置为\(w_i - 1\),有的位置为\(-1\),问有多少种排列,使得任意前缀和非负? 我们末尾加上一个\(-1\),就是要保证除了末尾外的前缀 ...
- SpringBoot web 小项目
Spring Boot 整合 Thymeleaf 完整 Web 案例 原创出处 作者:泥瓦匠BYSocket 希望转载,保留摘要,谢谢! Thymeleaf 是一种模板语言.那模板语言或模板引擎是什 ...
- 【noip2018】【luogu5021】赛道修建
题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,…,n1,2,…,n,有 n-1n−1 条适合于修建赛道的双向通 ...
- 【bzoj4811】由乃的OJ
Portal --> bzoj4811 Solution 这题可以用树剖+线段树做也可以用LCT做,不过大体思路是一样的 (接下来先讲的是树剖+线段树的做法,再提LCT的做法) 首先位 ...
- 如何在 ASP.NET 应用程序中实现模拟用户身份(在ASP.NET中以管理员身份运行网站)
前言 在实际的项目开发中,我们可能会需要调用一些非托管程序,而有些非托管程序需要有更高的身份权限才能正确执行.本文介绍了如何让IIS承载的ASP.NET网站以特定的账户执行,比如Administrat ...
- Flash平台的分析与RIA的趋势
10月3号,Flash Player 11 和 AIR 3.0正式提供下载,一片安静.最近这两年来,关于Flash的新闻一向是以负面为主,先是 Silverlight 的挑战,然后是 iphone和i ...
- 《剑指offer》— JavaScript(5)用两个栈实现队列
用两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 实现代码 function Stack(){ var item = []; this. ...
- sloop公共程序之初始过程及启动
1:sloop_init() 初始化主要是初始化静态sloop_*** 结构体和填充struct sloop_data 结构体中的成员. //初始化静态存储区给sloop_***结构体 static ...
- 使用rabbitmq消息队列
一.前言 在python中本身就是存在队列queue.一个是线程队列queue,另一个是进程multiprocessing中的队列Queue. 线程queue:只用于线程之间的数据交互 进程Queue ...