要求

  • 给定一个正数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. [Fundamental of Power Electronics]-PART I-3.稳态等效电路建模,损耗和效率-3.3 等效电路模型的构建

    3.3 等效电路模型的构建 接下来,让我们完善直流变压器模型来解决变换器的损耗问题.这将使用众所周知的电路分析技术来确定变换器的电压,电流和效率. 在前面的章节,我们利用电感伏秒平衡和电容电荷平衡得到 ...

  2. C#委托的学习了解

    C#的委托(Delegate)类似于C\C++的函数指针.委托是存有对某一个方法引用的一种引用变量类型,引用可在运行时被改变. 委托特别用于实现事件和回调方法.所有的委托都派生自System.Dele ...

  3. 华为联运游戏或应用审核驳回:HMS Core升级提示语言类型错误

    问题描述 最近项目组应用集成华为的HMS Core SDK相关能力后,发布地区选择中国大陆,提交审核,华为审核驳回:在低于2.5.3版本的华为移动服务手机上启动时或调出支付时拉起升级提示为英文,正确的 ...

  4. Spring Boot 2.4 配置文件将加载机制大变化

    Spring Boot 2.4.0.M2 刚刚发布,它对 application.properties 和 application.yml 文件的加载方式进行重构.如果应用程序仅使用单个 applic ...

  5. Dapr | 云原生的抽象与实现

    引言 Dapr 是微软主导的云原生开源项目,2019年10月首次发布,到今年2月正式发布 V1.0 版本.在不到一年半的时间内,github star 数达到了 1.2 万,超过同期的 kuberne ...

  6. js收藏展开与隐藏,返回顶部

    var a = document.getElementById("more");var b = document.getElementById("moreList&quo ...

  7. 织梦DedeCMS自定义表单diy_list.htm

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. android so加载

    本文分析so加载的步骤,其实在之前dalvik浅析二中也有提及,但那重点关注的是jni.android中so库的加载,代码如下: loadLibrary("nanosleep"); ...

  9. 详解 WebRTC 传输安全机制:一文读懂 DTLS 协议

    作者|进学 审校|泰一 DTLS (Datagram Transport Layer Security) 基于 UDP 场景下数据包可能丢失或重新排序的现实情况下,为 UDP 定制和改进的 TLS 协 ...

  10. Redis 的持久化

    原文链接:https://www.changxuan.top/?p=1386 Redis 是一个非关系型的内存数据库,使用内存存储数据是它能够进行快速存取数据的原因之一. 在实际应用中,常有人提倡把 ...