LeetCode题解 343.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.
解释:
先看个穷举的规律,本题最核心的技巧或者说思路是 将数字拆分成2和3的乘积,因为 2*3 > 1*5; 3*3 > 1*6; 3*2*2 > 1*7; 2*2*2*2 > 1*8 .......所以拆分成2和3后,就能得到最大的乘积。
2 :1 , 1
3 :1 , 2
4 :2 , 2
5 :3 , 2
6 :3 , 3
7 :3 , 2 , 2
8 :3 , 3 , 2
9 :3 , 3 , 3
10 :3 , 3 , 2, 2
代码:
public class Solution {
public int integerBreak(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
if (n % 3 == 0) return (int)Math.pow(3, n / 3);
if (n % 3 == 1) return (int)Math.pow(3, (n - 4) / 3) * 4; // 有多少的3和4可以根据上面的穷举找到规律
return (int)Math.pow(3, (n - 2) / 3) * 2;
}
}
LeetCode题解 343.Integer Break的更多相关文章
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- 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 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- 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@ [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 the ...
- (dp)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 ...
随机推荐
- .Net中关于等于的故事(一)
在.Net框架中,如果您查看所有类型的的基类:System.Object类,将找到如下4个与相等判断的方法: static Equals() virtual Equals() static Refer ...
- WCF学习——构建一个简单的WCF应用(一)
本文的WCF服务应用功能很简单,却涵盖了一个完整WCF应用的基本结构.希望本文能对那些准备开始学习WCF的初学者提供一些帮助. 在这个例子中,我们将实现一个简单的计算器和传统的分布式通信框架一样,WC ...
- angularJS directive详解(自定义指令)
Angularjs指令定义的API AngularJs的指令定义大致如下 其中return返回的对象包含很多参数,下面一一说明 1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式 ...
- jQuery.merge( first, second )返回: Array
jQuery.merge( first, second )返回: Array描述: 合并两个数组内容到第一个数组.first类型: Array第一个用于合并的数组,其中将会包含合并后的第二个数组的内容 ...
- 《JavaScript高级程序设计》笔记二
第二章 在HTML中使用JavaScript 要想把JavaScript放到网页中,就必须涉及到Web的核心语言HTML.向HTML页面中插入JavaScript的主要方法,就是使用<scrip ...
- 怎样用DOS命令创建txt文本文档
单击运行, 打开命令提示符. 例如在D盘创建文本文档,那么就先进入D盘,在后面写 D: 于是就进入了D盘怎样用DOS命令创建txt文本文档 然后在后面写命令 copy con 文件名.txt ,然后回 ...
- jQuery星级评分插件
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 利用宏定义实现C++程序在Unix和Win32环境下的通用性
[转] 1.1. 宏定义软件的代码,从跨平台的角度来看,可以分为平台相关的和平台无关的.采用C/C++编写的软件,在进行移植时,平台无关的的代码基本上不需要做大的改动,但平台相关的代码需要做很大的调整 ...
- java udp服务器设计源码
根据个人理解写的,轻喷. 没什么大的逻辑,直接上代码. UDPServer.java package mySocket;/* * 服务器端 */import java.awt.GridLayout;i ...