题目: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的更多相关文章

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

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

  2. 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 ...

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

  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 343. Integer Break(dp或数学推导)

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

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

  7. Leetcode 343. Integer Break

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

  8. (dp)343. Integer Break

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

  9. 343. Integer Break

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

随机推荐

  1. 在Caffe添加Python layer详细步骤

    本文主要讨论的是在caffe中添加python layer的一般流程,自己设计的test_python_layer.py层只是起到演示作用,没有实际的功能. 1) Python layer 在caff ...

  2. Nginx+Keepalived 主备高可用 安装与配置

    环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10. ...

  3. navicat连接oracle 报 ORA-12737 set CHS16GBK

    1首先,我们打开“工具”-->"选项"菜单,见到如下界面,依据OCI library(oci.dll) 路径,导航到 navicat oci 目录下,备份里面的文件(通过在该 ...

  4. JS 事件派发器EventDispatcher

    在Java和AS中经常用到EventDispatcher,写了一个JS版本的. addListener :添加事件监听器 removeListener:移除事件监听器 dispatchEvent:派发 ...

  5. UIPopoverPresentationController使用

    UIPopoverPresentationController是什么? iOS8.0之后引入的一个方便开发者创建带箭头的弹出控制器,类似qq消息页面点击右上角加号弹出的视图. UIPopoverPre ...

  6. ELK-Kibana-01

      1.Kibana介绍   Kibana 是一个设计使用和Elasticsearch配置工作的开源分析和可视化平台.可以用它进行搜索.查看.集成Elasticsearch中的数据索引.可以利用各种图 ...

  7. AngularJS的文字溢出处理

    需求大致如图所示,由于本人也是新接触AngularJS,研究好久,制作了一个demo,代码如下: <!DOCTYPE html><html><head><me ...

  8. centos yum 没有可用软件包 nginx。

    新装的centos7中没有nginx的镜像源 因为nginx位于第三方的yum源里面,而不在centos官方yum源里面 解决方案: 安装epel: 去epel官网: http://fedorapro ...

  9. jquery hide和show使用

    $("#qitarenyuanDiv").hide("fast");$("#qitarenyuanDiv").show("fast ...

  10. Petya勒索病毒疫苗出现,分分钟让电脑对病毒免疫

    继wannacry之后,Petya勒索软件攻击再次席卷全球,对欧洲.俄罗斯等多国政府.银行.电力系统.通讯系统.企业以及机场造成了不同程度的影响. 研究发现,Petya 会锁定磁盘的 MFT 和 MB ...