当前位置:网站首页>快来扔鸡蛋。
快来扔鸡蛋。
2022-08-09 12:45:00 【养猪去】
DP(也是一切优化的基础)
时间复杂度: O ( k ∗ n ∗ n ) O(k*n*n) O(k∗n∗n)
class Solution {
int N = 10010;
int K = 110;
public int superEggDrop(int k, int n) {
int[][] f = new int[K][N];
for(int j = 1; j <= n; j++) f[1][j] = j;
for(int i = 2; i <= k; i++) {
for(int j = 1; j <= n; j++) {
int res = N;
for(int t = 1; t <= j; t++) {
// t 的选择对应最优策略,策略应选择最优,所以是min
// 每种策略下,有多种可能(这里有两种可能),选择最差情况。
res = Math.min(res, Math.max(f[i - 1][t - 1], f[i][j - t]) + 1);
}
f[i][j] = res;
}
}
return f[k][n];
}
}
二分(决策单调性)优化
必须要认识到一点的是, f ( k , n ) f(k,n) f(k,n)是关于n的单调不减(或者说非严格单调增)函数。
class Solution {
int N = 10010;
int K = 110;
public int superEggDrop(int k, int n) {
int[][] f = new int[K][N];
for(int j = 1; j <= n; j++) f[1][j] = j; // 初始化; 且f[x][0] = 0;
for(int i = 2; i <= k; i++) {
int key = 1;
for(int j = 1; j <= n; j++) {
// f[i - 1][t - 1]、f[i][j - t])
while(key <= j && f[i - 1][key - 1] < f[i][j - key]) {
++key;
}
int res = f[i][j - (key - 1)]; // 两个临界点
if(key <= j) {
res = Math.min(res, f[i - 1][key - 1]);
}
f[i][j] = res + 1;
}
}
return f[k][n];
}
}
参考链接
边栏推荐
猜你喜欢
ViewPager fragments of nested data blank page abnormal problem analysis
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
Rust from entry to proficient 04 - data types
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol
5G China unicom AP:B SMS ASCII 转码要求
Redis源码剖析之字典(dict)
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
生成上传密钥和密钥库
WSA工具箱安装应用商店提示无法工作怎么解决?
随机推荐
ftplib+ tqdm upload and download progress bar
Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
ABP中的数据过滤器 (转载非原创)
GIN中GET POST PUT DELETE请求
JVM之配置介绍(一)
ARM board adds routing function
novel research
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
陈强教授《机器学习及R应用》课程 第十四章作业
Redis源码剖析之数据过期(expire)
WSA toolkit installed app store tip doesn't work how to solve?
基于 R 语言的判别分析介绍与实践 LDA和QDA
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
Rust从入门到精通04-数据类型
Data Mining-06
生成上传密钥和密钥库
R语言kaggle 游戏数据探索与可视化
30行代码实现微信朋友圈自动点赞
Redis源码剖析之robj(redisObject)
电脑重装系统还原0x80070005错误如何解决