当前位置:网站首页>[jzof] 14 cut rope
[jzof] 14 cut rope
2022-07-28 10:08:00 【Sighed, angry】

Recurrence of violence :
// Method 1, Recurrence of violence , The time complexity is O(n!), The space complexity is O(n), At most n paragraph , The length of each segment is 1, The recursion depth is n
public static int cutRope(int target) {
if (target == 2) {
return 1;
} else if (target == 3) {
return 2;
} else {
return backTrack(target);
}
}
private static int backTrack(int n) {
// n<=4, Indicates that there is no division , The length is the longest .
if (n <= 4) {
return n;
}
int res = 0;
for (int i = 1; i < n; i++) {
res += Math.max(res, i * backTrack(n - i));
}
return res;
}
Memory recursion :
// Memory recursion , The time complexity is O(n^2), The space complexity is O(n)
static int[] mark = null;
public static int cutRope2(int target) {
if (target == 2) {
return 1;
} else if (target == 3) {
return 2;
}
// Create an array , The initial value is 0
mark = new int[target+1];
return backTrack2(target);
}
private static int backTrack2(int target) {
if (target <= 4) {
return target;
}
if (mark[target] != 0) {
return mark[target];
}
int ret = 0;
for (int i = 1; i < target; i++) {
ret = Math.max(ret, i * backTrack2(target - i));
}
return mark[target] = ret;
}
Dynamic programming :
// Dynamic programming , The time complexity is O(n^2), The space complexity is O(n)
public static int cutRope3(int target) {
if (target == 2) {
return 1;
} else if (target == 3) {
return 2;
}
int[] mark = new int[target + 1];
for (int i = 1; i <= 4; i++) {
mark[i] = i;
}
for (int i = 5; i <= target; i++) {
for (int j = 1; j < i; j++) {
mark[i] = Math.max(mark[i], j * mark[i - j]);
}
}
return mark[target];
}
Reference article :
https://blog.csdn.net/arpospf/article/details/110492761
边栏推荐
- LinkedList源码按摩,啊舒服
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- Array collation commonly used in PHP
- ThresholdFilter简介说明
- Introduction to timebasedrollingpolicy
- PHP 基础
- Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
- 【MySQL】Got an error reading communication packets
- 总线相关概念集合
- 一文读懂Plato Farm的ePLATO,以及其高溢价缘由
猜你喜欢

高温天气筑牢安全生产防线,广州海珠区开展加油站应急演练

房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升
![[esp32][esp idf] esp32s3 quickly build lvglv7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[esp32][esp idf] esp32s3 quickly build lvglv7.9

OSPF的拓展配置,选路原则,防环及重发布

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03

并查集

Digital construction of pharmaceutical industry is on the verge

OSPF的LSA及优化

建筑建材行业B2B电子商务网站方案:赋能建材企业转型升级,实现降本提效

OSS direct upload rails service practice
随机推荐
Boss: there are too many systems in the company. Can we realize account interworking?
Redis面试题必知必会
web之圣杯和双飞翼布局、float、clear、both
【FPGA教程案例41】图像案例1——通过verilog读取图片
Read Plato farm's eplato and the reason for its high premium
Sizebasedtriggingpolicy introduction
高温持续,公交企业开展安全专项培训
Which strings will be resolved to null by fastjason?
In the era of home health diagnosis, Senzo creates enhanced lateral flow test products
Basic examples that must be mastered by beginners of C #
OSPF的LSA及优化
Seektiger eco pass STI new progress, log in to ZB on April 14
Linux操作系统(Centos7)安装MySQL
(10) Defer keyword
OpenAtom OpenHarmony分论坛,今天14:00见!附大事记精彩发布
3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities
This wechat plug-in is very easy to use
Installing MySQL for Linux operating system (centos7)
Take you to wechat applet development in 3 minutes
Install lamp under deepin