LeetCode-Integer Breaks
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 and not larger than 58.
Analysis:
Interesting problem. The solution is to create as many 3 as possible.
Solution:
public class Solution {
public int integerBreak(int n) {
if (n==2) return 1;
if (n==3) return 2;
if (n==4) return 4;
if (n==5) return 6; return 3*Math.max(n-3,integerBreak(n-3));
}
}
LeetCode-Integer Breaks的更多相关文章
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode "Integer Break"
A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...
- LeetCode Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to it ...
- Leetcode Integer Replacement
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- Leetcode Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- LeetCode: Integer to Roman 解题报告
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
随机推荐
- python3 logging 日志记录模块
#coding:utf-8 import logginglogging.basicConfig(filename='log1.log', format='%(asctime)s -%(name)s-% ...
- C#面试基础问题0
传入某个属性的set方法的隐含参数的名称是什么?value,它的类型和属性所声名的类型相同. 如何在C#中实现继承?在类名后加上一个冒号,再加上基类的名称. C#支持多重继承么?不支持.可以用接口来实 ...
- memcahced&redis命令行cmd下的操作
一.memcahced 1.安装 执行memcached.exe -d install 把memcached加入到服务中 执行memcached.exe -d uninstall 卸载memcac ...
- linux下使用dd命令写入镜像文件到u盘
1.使用 df -h ,查看一下当前各个磁盘 user@host ~/ $ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 119 ...
- JNI调用实例
1. 环境 Windows7-64Bit VS2010-32Bit JDK1.8-64Bit 2. 步骤 2.1 创建NativePrint类 public class NativePrint { p ...
- Atitit.软件开发的几大规则,法则,与原则。。。attilax总结
Atitit.软件开发的几大规则,法则,与原则... 1. 设计模式六大原则 2 1.1. 设计模式六大原则(1):单一职责原则 2 1.2. 设计模式六大原则(2):里氏替换原则 2 1.3. 设计 ...
- 设置phpcms v9黄页模块作为首页方法
如果我们根据需要,想把黄页作为单独的网站,我们可以用模块化安装,并且首页设置,那么仿站网就说说详细的步骤.首先,我们需要安装最新版本的phpcms V9其次,下载黄页模块,然后进行根目录的替换.再次, ...
- 并发insert情况下会发生重复的数据插入问题
1.背景 用多线程接收推送的订单数据,把接收的订单数据存到一个表中,实现的需求是:如果接收的订单消息在数据库中已经存在,那么执行update操作:如果没有存在,那么执行insert操作代码逻辑: if ...
- EM5-PE6B
1. vocabulay 1.1 scary adj.令人恐怖的 This is a scary thing. you can construct some extremely scary scena ...
- c# 多线程里面创建byte数组发生内存溢出异常求解
在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...