要求

  • 给定一个正数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. go的令牌桶实现库 go-rate

    关于我 我的博客|文章首发 go-rate是速率限制器库,基于 Token Bucket(令牌桶)算法实现. go-rate被用在LangTrend的生产中 用于遵守GitHub API速率限制. 速 ...

  2. ThoughtWorks首席咨询师带你一站通关中台

    大家都在谈中台,是当下一个热议的话题,但是我们最关心的两个基本问题还是没有答案.一个是中台的概念,依然是见仁见智,始终没有一个统一的见解:另一个是中台的落地,更是鲜有人谈. 拨开当下有关中台的层层迷雾 ...

  3. 华为云PB级数据库GaussDB(for Redis)揭秘第八期:用高斯 Redis 进行计数

    摘要:高斯Redis,计数的最佳选择! 一.背景 当我们打开手机刷微博时,就要开始和各种各样的计数器打交道了.我们注册一个帐号后,微博就会给我们记录一组数据:关注数.粉丝数.动态数-:我们刷帖时,关注 ...

  4. C/C++中的字符串相关姿势

    这是我在<程序设计实习>课程上作的pre,目标是对C/C++中字符串的相关内容作一个尽量完整的介绍.(对于OIer可能不太实用) 课件链接: https://files.cnblogs.c ...

  5. JMeter发送get请求并分析返回结果

    在实际工作的过程中,我们通常需要模拟接口,来进行接口测试,我们可以通过JMeter.postman等多种工具来进行接口测试,但是工具的如何使用对于我们来说并不是最重要的部分,最重要的是设计接口测试用例 ...

  6. Day11_58_增强for循环

    增强for循环 * 语法 : for(数据类型 变量名:数组名/集合名) * 集合如果要使用增强for循环需要先使用泛型来确定元素类型,如果没有使用泛型就使用foreach,那么变量类型设置为Obje ...

  7. Day07_33_链表

    链表 单链表 双向链表 * 什么是双向链表? 双向链表是链表的一种,由节点组成,每个数据结点中都有两个指针,分别指向直接后继和直接前驱. ![](https://img2020.cnblogs.com ...

  8. Scrapy的流程

    Scrapy框架的架构如下图 具体部分说明: Engine:引擎,处理整个系统的数据流处理,出发事物,是整个框架的核心 Item:项目.定义了爬取结果的数据结构,爬取的数据会被赋值成该Item对象 S ...

  9. 技术分享|SQL和 NoSQL数据库之间的差异:MySQL(VS)MongoDB

    在当今市场上,存在各种类型的数据库,选择适合你业务类型的数据库对应用的开发和维护有着重要意义.本篇文章,将为大家分享SQL和NoSQL语言之间的区别,同时还将比较这两种类型的数据库,以帮助小伙伴们选择 ...

  10. 13- APP接口测试以及postman使用

    postman安装与操作 ---------------------- 接口操作图片 -------------------- 一.postman操作key值:来源于聚合   请求-->聚合-- ...