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. HttpServletRequest接口实例化的使用

    HttpServletRequ接口的使用和jsp内置对象的request对象非常类似,request对象其实 就是HttpServletRequest接口的一个实例,不过气实例化的过程是自动的,无须自 ...

  2. 查看Linux版本系统信息方法汇总

    Linux下如何查看版本信息, 包括位数.版本信息以及CPU内核信息.CPU具体型号等等,整个CPU信息一目了然.  1.# uname -a   (Linux查看版本当前操作系统内核信息)  Lin ...

  3. 图像二值化----otsu(最大类间方差法、大津算法)

    最大类间方差法是由日本学者大津于1979年提出的,是一种自适应的阈值确定的方法,又叫大津 法,简称OTSU.它是按图像的灰度特性,将图像分成背景和目标2部分.背景和目标之间的类间方差越大,说明构成图像 ...

  4. !!无须定义配置文件中的每个变量的读写操作,以下代码遍历界面中各个c#控件,自动记录其文本,作为配置文件保存

    namespace PluginLib{    /// <summary>    /// 遍历控件所有子控件并初始化或保存其值    /// </summary>    pub ...

  5. 转 Android中进入系统设置界面

    Android软件时,常常需要打开系统设置或信息界面,来设置相关系统项或查看系统的相关信息,这时我们就可以使用以下语句来实现:(如打开“无线和网络设置”界面) Intent intent = new ...

  6. 面试题_76_to_81_Java 最佳实践的面试问题

    包含 Java 中各个部分的最佳实践,如集合,字符串,IO,多线程,错误和异常处理,设计模式等等. 76)Java 中,编写多线程程序的时候你会遵循哪些最佳实践?(答案)这是我在写Java 并发程序的 ...

  7. git文件未改动pull的时候提示冲突

    今天在mac下使用git工具,出现一个很奇怪的问题. 先声明当前工作目录是干净的,运行 git status 没有任何文件改动,且没有任何需要push的文件. 我执行 git pull 命令,直接提示 ...

  8. scrapy wiki资料汇总

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

  9. maximum-gap(经过了提示)

    下面的分桶个数做的不太好,原来的解法是用的 int gap = (big - small) / vlen; if (gap == 0) { gap = 1; } 下面是现在的Java解法: packa ...

  10. Android自动化测试之Monkeyrunner从零开始(三)

    转自http://www.51testing.com/html/81/22381-854342.html 时光过得太快了,一晃离上一篇monkeyrunner系列的博客已经一年多了.这一年多时间经历了 ...