要求

  • 给定一个正数n,可将其分割成多个数字的和,求让这些数字乘积最大的分割方法(至少分成两个数)

示例

  • n=2,返回1(2=1+1)
  • n=10,返回36(10=3+3+4)

实现

  • 回溯遍历(n^2,超时)

 1 class Solution {
2 private:
3 int max3( int a , int b , int c ){
4 return max( a , max(b,c) );
5 }
6
7 // 将n进行分割(至少两部分)可获得的最大乘积
8 int breakInteger(int n){
9
10 if( n == 1 )
11 return 1;
12
13 int res = -1;
14 for( int i = 1 ; i <= n-1 ; i ++ )
15 // i + (n-i)
16 res = max3( res, i * (n-i) , i * breakInteger(n-i) );
17
18 return res;
19 }
20
21 public:
22 int integerBreak(int n) {
23 return breakInteger(n);
24 }
25 };
  • 记忆化搜索

    • 21:不要写成 res=max(res,i*breakInteger(n-i)),breakInteger(n-i) 将 n-i 至少分成两部分,不分割的话就是 i*(n-i)
    • 自定义传入3个数求最大值的函数

 1 class Solution {
2 private:
3 vector<int> memo;
4
5 int max3( int a , int b , int c ){
6 return max( a , max(b,c) );
7 }
8
9 // 将n进行分割(至少两部分)可获得的最大乘积
10 int breakInteger(int n){
11
12 if( n == 1 )
13 return 1;
14
15 if( memo[n] != -1)
16 return memo[n];
17
18 int res = -1;
19 for( int i = 1 ; i <= n-1 ; i ++ )
20 // i + (n-i)
21 res = max3( res, i * (n-i) , i * breakInteger(n-i) );
22 memo[n] = res;
23 return res;
24 }
25
26 public:
27 int integerBreak(int n) {
28 memo = vector<int>(n+1,-1);
29 return breakInteger(n);
30 }
31 };
  • 动态规划

    • 重叠子问题:有相同的子问题,可采用记忆化搜索进行优化
    • 最优子结构:通过求子问题的最优解,可以获得原问题的最优解
    • 如:想获得分割n的最大乘积,需要知道分割n-1,n-2...,1等的最大乘积
    • 满足重叠子问题 + 最优子结构的递归问题,可以用记忆化搜索/动态规划求解

 1 class Solution {
2 private:
3 int max3( int a , int b , int c ){
4 return max( a , max(b,c) );
5 }
6
7 public:
8 int integerBreak(int n) {
9 assert( n >= 2 );
10
11 // memo[i]表示至少将数字i分割(至少两部分)后得到的最大乘积
12 vector<int> memo(n+1,-1);
13
14 memo[1] = 1;
15 for( int i = 2 ; i <= n ; i ++ )
16 // 求解memo[j]
17 for( int j = 1 ; j <= i-1 ; j ++ )
18 // j + (i-j)
19 memo[i] = max3(j*(i-j) , j*memo[i-j] , memo[i] );
20
21 return memo[n];
22 }
23 };

相关

  • 279 Perfect Squares
  • 91 Decode Ways
  • 62 Unique Paths
  • 63 Unique Paths II

[刷题] 343 Integer Break的更多相关文章

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

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

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

  4. 343. Integer Break -- Avota

    问题描述: Given a positive integer n, break it into the sum of at least two positive integers and maximi ...

  5. [LeetCode] 343. Integer Break 整数拆分

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

  6. (dp)343. Integer Break

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

  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. 343. Integer Break

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

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

随机推荐

  1. day-01-初识Python与条件判断

    cpu 内存 硬盘 操作系统 cpu:计算机的运算和计算中心,相当于人类大脑.飞机 ​ 内存:暂时存储数据,临时加载数据应用程序,4G,8G,16G,32G ​ 速度快,高铁,断电即消失.造价很高 ​ ...

  2. Markdown部分用法总结

    1.Markdown数学公式&符号 2.Cmd Markdown 公式指导手册

  3. java面试-线程池使用过吗,谈谈对ThreadPoolExector的理解

    一.架构说明: 二.为什么使用线程池,优势是什么? 线程池做的工作主要是控制运行的线程的数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,那么超出数量的线程排队 ...

  4. java.net.BindException: Problem binding to [hadoop103:8031] java.net.BindException

    ResourceManger启动失败,Namenode启动成功,这个问题排查了好久 在hadoop-2.7.6/logs/yarn-root-resourcemanager-hadoop102.log ...

  5. OOUnit3Summary

    一.JML基础梳理及工具链 jml语言基础 JML的全称是Java Modeling language,是一种行为接口规格语言,通过JML及其支持工具,不仅可以基于规格自动构造测试用例,还可用SMT ...

  6. Java 时间日期系列

    Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(1) Calendar Java Calendar,Date,DateFormat ...

  7. 「新特性」Spring Boot 全局懒加载机制了解一下

    关于延迟加载 在 Spring 中,默认情况下所有定的 bean 及其依赖项目都是在应用启动时创建容器上下文是被初始化的.测试代码如下: @Slf4j @Configuration public cl ...

  8. 鹏城杯_2018_treasure

    鹏城杯_2018_treasure 首先检查一下保护: IDA分析 我们先来看看settreasure()函数 申请了两个内存空间,并往sea中复制了shellcode 看看这个shellcode,不 ...

  9. 数据结构之栈(JavaScript描述)

    栈数据结构   栈是一种遵从后进先出原则的有序集合.新添加或待删除的元素都保存在栈的同一端,称为栈顶,另一端就叫栈底.在栈内,锌元素都靠近栈顶,救援都接近栈底 类似栈的例子   栈也被用在编程语言你的 ...

  10. WebPack系列--开启HappyPack之后,再将项目打包速度缩短5秒

    效果展示 打包时间:缩短了 26.296s-20.586s=5.71s 先看两组测试数据,第一组是没有使用DllPlugin的打包测试数据,测量三次取平均值是26.296s(25.72+25.56+2 ...