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. java对称加密报错:Input length must be multiple of 8 when decrypting with padded cipher

    HTTP Status 500 - Request processing failed; nested exception is javax.crypto.IllegalBlockSizeExcept ...

  2. JCIFS是很不稳定的

    我以前也试过这样登录失败,第二天就能登录成功了. JCIFS是很不稳定的. 如果是域登录可以这样 //DOMAIN_IP         域名服务(其实域名和域名服务器IP可以,不过用IP解析速度快很 ...

  3. Java API —— BigInteger类

    1.BigInteger类概述        可以让超过Integer范围内的数据进行运算 2.构造方法     public BigInteger(String val) 3.BigInteger类 ...

  4. hadoop拾遗(一)---- 避免切分map文件

    有些程序可能不希望文件被切分,而是用一个mapper完整处理每一个输入文件.例如,检查一个文件中所有记录是否有序,一个简单的方法是顺序扫描第一条记录并并比较后一条记录是否比前一条要小.如果将它实现为一 ...

  5. Linux批量杀进程

    ps -ef |grep 进程名|grep -v grep |awk '{print $2}' |xargs kill -9

  6. scrapy wiki资料汇总

    See also: Scrapy homepage, Official documentation, Scrapy snippets on Snipplr Getting started If you ...

  7. objcopy

    objcopy objcopy [options] infile [outfile] Copy the contents of the input object file to another fil ...

  8. drop.delete.trauncat的区别

    delete删除数据,保留表结构,可以回滚,如果数据量大,很慢,回滚是因为备份了删除的数据(删除数据时有两个动作,删除和备份) truncate删除所有数据,保留表结构,不可以回滚,一次全部删除所有数 ...

  9. 函数fsp_get_space_header

    /**********************************************************************//** Gets a pointer to the sp ...

  10. socket编程---一个简单例子

    服务器端代码(单线程): import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe ...