https://leetcode.com/problems/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.

Hint:

Show Hint

  1. There is a simple O(n) solution to this problem.Show More Hint
  2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.
public class Solution {
public int integerBreak(int n) { if(n <= 2) {
return 1;
} else if (n == 3) {
return 2;
} double radical = Math.sqrt((double) n);
int rad = (int) Math.floor(radical); int rs = 0, power = 0;
for(int i=1; i<=rad+1; ++i) {
int times = n / i;
int remain = n - i*times; if(remain == 0) {
power = (int) Math.pow(i, times);
rs = Math.max(rs, power);
} else {
int power_1 = (int) Math.pow(i, times-1) * (remain + i);
int power_2 = (int) Math.pow(i, times) * remain;
rs = Math.max(rs, Math.max(power_1, power_2));
}
} return rs;
}
}

leetcode@ [343] Integer Break (Math & Dynamic Programming)的更多相关文章

  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. [LeetCode] 343. Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  3. leetcode 343. Integer Break(dp或数学推导)

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  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. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  6. #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 ...

  7. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  8. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

  9. LeetCode题解 343.Integer Break

    题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...

随机推荐

  1. NSArray 初始化

    //NSArray长度不可变所以初始化的时候就赋值,并且最后以nil结尾 //此外需要注意NSArray不能存放C语言的基础类型 NSObject *obj=[[NSObject alloc]init ...

  2. Bean不同配置方式比较

      基于XML配置 基于注解配置 基于Java类配置 Bean定义 在XML文件中通过<bean>元素定义Bean,如:<bean class="com.bbt.UserD ...

  3. HBase的Shell操作

    1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...

  4. linux文件和目录基本操作

    比较特殊的目录: .   代表此层目录 .. 代表上一层目录 - 代表前一个工作目录 -代表当前用户身份所在的主文件夹 -account 代表account用户所在主文件夹 1.目录相关操作 cd切换 ...

  5. poi2012完成

    终于完成了(2798是我cheat的……),感觉poi的题好锻炼智商…… 截图留念,题解见博客中对应题号的解题报告

  6. shape的属性(二)

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="r ...

  7. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

  8. django - 好的 获取 参数值 方法

    第一步: # 参数列表 parameters = ('user_id', 'day_time', 'normal_data', 'hourly_data', 'product_id') # 需要传入的 ...

  9. 一个P2P点播直播开源项目:P2PCenter

    最近跟着公司的项目走,我也研究了不少东西,尤其是在P2P方面,广泛涉猎各种开源项目,尤其是国外的开源项目,意外的发现了一个国内的项目,做的还不错,推荐一下.---------------------使 ...

  10. 海康、大华IpCamera RTSP地址和格式

    海康:rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream说明:username: 用户名.例如 ...