当前位置:网站首页>[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
边栏推荐
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- OSPF的不规则区域,LSA和序列号
- Holy Grail of web and double wing layout, float, clear, both
- Array collation commonly used in PHP
- ThresholdFilter简介说明
- 13 probability distributions that must be understood in deep learning
- Several innovative economic models of platofarm have inspired the current metacosmic market
- 总线相关概念集合
- Plato Farm-以柏拉图为目标的农场元宇宙游戏
- Description of landingsite electronic label quppa firmware entering DFU status
猜你喜欢
Edge team explains how to improve the comprehensive performance experience through disk cache compression technology

Joint search set

二分、三分、01分数规划【第III弹】

Flink - checkpoint Failure reason: Not all required tasks are currently running

为什么要考一级建造师,一建证书含金量有多高?

Read Plato farm's eplato and the reason for its high premium

技术人 | 研发效能的思考总结

工业品MRO采购网站有哪些优势?一文带你读懂

OSPF的LSA及优化

Plato Farm-以柏拉图为目标的农场元宇宙游戏
随机推荐
每天在岗不足8小时被辞?腾讯前员工追讨1300万加班费等,法院终审获赔9万
JWT login authentication + token automatic renewal scheme, well written!
【JZOF】14剪绳子
深度学习必懂的 13 种概率分布
LinkedList源码按摩,啊舒服
Array collation commonly used in PHP
Deepin 下安装 LAMP
PHP connection MySQL native code
二分、三分、01分数规划 【第I弹】
FixedWindowRollingPolicy简介说明
Boss: there are too many systems in the company. Can we realize account interworking?
7.27 minimum spanning tree phased test problem solution
TCP 基础知识
JWT 登录认证 + Token 自动续期方案,写得太好了!
老板:公司系统太多,能不能实现账号互通?
MySQL 为什么有时候会选错索引?
[OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)
Net 3 lines of code to realize the function of text to speech
房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升
Read Plato farm's eplato and the reason for its high premium