当前位置:网站首页>快来扔鸡蛋。
快来扔鸡蛋。
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];
}
}
参考链接
边栏推荐
- Clock frequency and baud rate count for serial communication in FPGA
- 透明tune proxy
- 使用RecyclerView实现三级折叠列表
- FPGA中串口通信的时钟频率和波特率计数
- 5G China unicom repeater network management protocol real-time requirements
- 正则引擎的几种分类
- 关于Retrofit网络请求URL中含有可变参数的处理
- leetcode 20. Valid Parentheses 有效的括号(中等)
- 驻波比计算方法
- ftplib+ tqdm upload and download progress bar
猜你喜欢
How to save Simulink simulation model as image or PDF
5G Unicom Network Management Design Ideas
5G China unicom AP:B SMS ASCII Transcoding Requirements
Periodic sharing of Alibaba Da Tao system model governance
GIN Bind模式获取参数和表单验证
Unicom network management protocol block diagram
用场景定义硬件,英码科技破解“边缘计算”密码
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
RTSP协议讲解
5G China unicom AP:B SMS ASCII 转码要求
随机推荐
透明tune proxy
The FPGA - work summary recently
基于 R 语言的深度学习——简单回归案例
R语言kaggle 游戏数据探索与可视化
How to reduce the size of desktop icons after the computer is reinstalled
激光熔覆在农机修复强化中的应用及研究方向
GIN中GET POST PUT DELETE请求
在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
中断系统结构及中断控制详解
5G China unicom 直放站 网管协议 实时性要求
uni-app - uview Swiper 轮播图组件点击跳转链接(点击后拿到 item 行数据, 取出数据做操作)
Intra-group reverse order adjustment of K nodes
Periodic sharing of Alibaba Da Tao system model governance
Flutter Getting Started and Advanced Tour (2) Hello Flutter
农村区县域农业电商如何做?数字化转型如何进行?
kustomize entry example and basic syntax instructions
驻波比计算方法
telnet+ftp 对设备进行 操控 和 升级
ansible-cmdb friendly display ansible collects host information
保存Simulink仿真模型为图片或者PDF的方法