题目: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. 【论文:麦克风阵列增强】An alternative approach to linearly constrained adaptive beamforming

    作者:桂. 时间:2017-06-03  21:46:59 链接:http://www.cnblogs.com/xingshansi/p/6937259.html 原文下载:http://pan.ba ...

  2. angular嵌入注入服务实例

    <!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...

  3. Facebook开源Zstandard新型压缩算法代替Zlib 简单使用

    简介 Zstandard(缩写为Zstd)是由Facebook的Yann Collet开发的一个无损数据压缩算法.Zstandard在设计上与DEFLATE(.zip.gzip)算法有着差不多的压缩比 ...

  4. Java中Bean是什么

    javaBean在MVC设计模型中是model,又称模型层,在一般的程序中,我们称它为数据层,就是用来设置数据的属性和一些行为,然后我会提供获取属性和设置属性的get/set方法JavaBean是一种 ...

  5. python str转dict

    两种方法 捷径 eval(str) >>> user = "{'name' : 'jim', 'sex' : 'male', 'age': 18}" >&g ...

  6. JS读写浏览器cookie及读取页面参数

    JS读写浏览器cookie及读取页面参数 var zbrowser = { //设置浏览器cookie,exdays是cookie有效时间 setCookie: function (c_name, v ...

  7. Linux 中最常用的目录及文件管理命令

    一.查看文件的命令 对于一个文本文件,在linux中有多种查看方式来获知文件内容,如直接显示整个文本内容.分页查看内容.或者只查看文件开头或末尾的部分内容.在linux可以用不同的命令来实现. 1. ...

  8. java数组中取出最大值

    class Demo{ public static void main(String []args){ int[] arr={3,54,456,342,2798}; int max=getMax(ar ...

  9. JavaScript中的排序

    <script> //1. 冒泡排序 function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len ...

  10. Linux 内核综述

    一.什么是Linux内核: 内核->操作系统中最重要的部分,内核将在系统引导时被装载进RAM,其中包含了很多关键的例程,以操作系统.内核是OS最为关键的部分,人们常将OS(操作系统)与内核等同. ...