当前位置:网站首页>Leetcode skimming: dynamic programming 06 (integer splitting)
Leetcode skimming: dynamic programming 06 (integer splitting)
2022-07-25 07:08:00 【Taotao can't learn English】
343. integer partition
Given a positive integer n, Split it into at least two positive integers and , And maximize the product of these integers . Return the maximum product you can get .
Example 1:
- Input : 2
- Output : 1
- explain : 2 = 1 + 1, 1 × 1 = 1.
Example 2:
- Input : 10
- Output : 36
- explain : 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
- explain : You can assume n Not less than 2 And not more than 58.

If I know the result .
class Solution {
public int integerBreak(int n) {
int[] arr = new int[]{
1, 2, 4, 6, 9, 12, 18, 27, 36, 54, 81, 108, 162, 243, 324, 486, 729, 972, 1458
, 2187, 2916, 4374, 6561, 8748, 13122, 19683, 26244, 39366, 59049, 78732, 118098, 177147
, 236196, 354294, 531441, 708588, 1062882, 1594323, 2125764, 3188646, 4782969, 6377292, 9565938
, 14348907, 19131876, 28697814, 43046721, 57395628, 86093442, 129140163, 172186884, 258280326, 387420489, 516560652, 774840978, 1162261467, 1549681956};
return arr[n - 2];
}
}

Dynamic programming
public static 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 * 2 <= i; j++) {
// More than half of the re segmentation is still repeated
// dp[i] Record the current maximum
// dp[i-j]*j dp[i-j] It can be regarded as the maximum product after segmentation
// (i-j)*j The product of two numbers
dp[i] = Math.max(dp[i], Math.max(dp[i - j] * j, (i - j) * j));
}
}
return dp[n];
}

Greedy Algorithm , This is mathematically proven .
边栏推荐
- Rongyun launched a real-time community solution and launched "advanced players" for vertical interest social networking
- BOM概述
- Basic usage of thread class
- 【愚公系列】2022年7月 Go教学课程 016-运算符之逻辑运算符和其他运算符
- Kyligence Li Dong: from the data lake to the index middle stage, improve the ROI of data analysis
- Leetcode 206. reverse linked list I
- Discuss the important factors that affect the success or failure of automated testing
- 数据提交类型 Request Payload 与 Form Data 的区别总结
- 分层强化学习综述:Hierarchical reinforcement learning: A comprehensive survey
- 蔚来一面:多线程join和detach的区别?
猜你喜欢

阿里云镜像地址&网易云镜像

Rongyun launched a real-time community solution and launched "advanced players" for vertical interest social networking

GIS实战应用案例100篇(十七)-基于DEM制作三维地图

微信小程序switchTab传参以及接收参数

Leetcode sword finger offer brush question notes

What are the hazards of insufficient sleep?

Can communication test based on STM32: turn the globe
![[daily question 1] 1184. Distance between bus stops](/img/36/2bbb8cc2a1fdd08070a5df7527e692.png)
[daily question 1] 1184. Distance between bus stops

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary problem solution (incomplete)

%d,%s,%c,%x
随机推荐
Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
Traffic is not the most important thing for the metauniverse. Whether it can really change the traditional way of life and production is the most important
The ultimate difference between MVC and three-tier architecture
Shell run command
Kyligence Li Dong: from the data lake to the index middle stage, improve the ROI of data analysis
Tp5.1 foreach adds a new field in the controller record, and there is no need to write all the other fields again without changing them (not operating in the template) (paging)
Devops has been practiced for many years. What is the most painful thing?
Dart final and const variables
分层强化学习综述:Hierarchical reinforcement learning: A comprehensive survey
Dynamic memory management
10 minutes to understand how JMeter plays with redis database
Builder pattern
微生物健康,不要排斥人体内微生物
CTF Crypto---RSA KCS1_ Oaep mode
runtimecompiler 和 runtimeonly是什么
[add, delete, modify, and check the array base]
Oracle table creation statement template
Wechat applet switchtab transmit parameters and receive parameters
【电脑讲解】NVIDIA发布GeForce RTX SUPER系列显卡,游戏玩家福利来了!
100 GIS practical application cases (seventeen) - making 3D map based on DEM
