当前位置:网站首页>LeetCode_343_整数拆分
LeetCode_343_整数拆分
2022-07-28 17:05:00 【Fitz1318】
题目链接
题目描述
给定一个正整数 n ,将其拆分为 k个 正整数 的和( k >= 2 ),并使这些整数的乘积最大化。
返回 你可以获得的最大乘积 。
示例 1:
输入: n = 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1。
示例 2:
输入: n = 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。
提示:
2 <= n <= 58
解题思路
动态规划法
- 确定dp数组以及下标的含义
dp[i]:将i拆分为至少两个正整数的和之后,这些正整数的最大乘积
- 确定递推公式
- 当
i >= 2时,假设将其拆分出的第一个正整数是j,剩余部分为i-j,则有两种可能:- 不再拆分
i-j,此时的乘积是j * (i - j) - 继续拆分
i-j,此时的乘积是j * dp[i-j]
- 不再拆分
- 因此当
j固定时,有dp[i] = max(j * (i - j), j * dp[i-j]) - 因为
1 <= j <= i-1,所以需要遍历所有的j得到dp[i]的最大值
- 当
- dp数组的初始化
dp[0] = dp[1] = 0,dp[2] = 1
- 确定遍历顺序
1 <= j <= i-1
AC代码
class Solution {
public int integerBreak(int n) {
int[] dp = new int[n + 1];
dp[2] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
dp[i] = Math.max(dp[i], Math.max(j * (i - j), j * dp[i - j]));
}
}
return dp[n];
}
}
边栏推荐
- 网络RJ45接口详解
- cout.write的学习
- Wired: who owns the art of the future? Openai allows dall-e users to commercialize their works. At present
- MongoDB数据库复制表
- Internet intelligence, how to define the next generation network transformation
- UE5 GAS 学习笔记 1.1能力系统组件Ability System Component
- 深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
- Mongodb create index
- Composition and principle of vector network analyzer (vector network)
- 食品安全 | 面包含盐量也会超标?几招教你正确吃面包!
猜你喜欢

DC-DC switching power supply

示波器探头详解

直播|StarRocks 技术内幕 :低基数全局字典优化

Brief introduction to the principle of spectrometer II

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

食品安全 | 面包含盐量也会超标?几招教你正确吃面包!

"Cloud strategy" will become an important pillar of enterprise digital transformation

Introduction to USB type-C PD fast charging

Calibration of vector network analyzer (vector network)

当Golang遇到高并发秒杀
随机推荐
VSC writes go, expected 'package' appears, found 'EOF‘
Experimental building - PHP Dafa
Brief introduction to the principle of spectrometer II
solidity转账函数的实现(基于transfer)
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
UE5 GAS 学习笔记 1.3属性Attribute
ADS仿真 之 直流仿真示例
天线的主要参数介绍
网络RJ45接口详解
There is a special cryptology language called asn.1
Random talk on GIS data (VI) - projection coordinate system
Is it really realistic that people who have not been exposed to software testing can take up their posts after two months of training?
DC simulation example of ADS simulation
haproxy实现灰度发布
LVS手册
频谱仪原理简介二
Detailed explanation of oscilloscope parameters
Introduction to advanced design system (ads) 2009 RF simulation
Wired: who owns the art of the future? Openai allows dall-e users to commercialize their works. At present
UE5 GAS 学习笔记 1.6 技能Gameplay Ability