guess-number-higher-or-lower-ii
// https://discuss.leetcode.com/topic/51353/simple-dp-solution-with-explanation
// https://en.wikipedia.org/wiki/Minimax
// 开始我的思路有问题,我是先选择区间,最后收敛到结果数
// 实际上work的思路是,先选择数字,再走向某个区间,然后取两个区间中的更大值 class Solution {
int ** table;
int DP(int s, int e) {
if (s >= e) {
return ;
} if (table[s][e] != INT_MAX) {
return table[s][e];
}
int local_max = INT_MAX;
for (int k=s; k<=e; ++k) {
// 下面这个表达式很重要
local_max = min(k + max(DP(s, k-), DP(k+, e)), local_max);
}
table[s][e] = local_max;
return local_max;
} public:
int getMoneyAmount(int n) { table = new int*[n+];
for (int i=; i<n+; ++i) {
table[i] = new int[n+];
for (int j=; j<n+; ++j) {
table[i][j] = INT_MAX;
}
} int ret = DP(, n); for (int i=; i<n+; ++i) {
delete[] table[i];
}
delete[] table; return ret;
} };
guess-number-higher-or-lower-ii的更多相关文章
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- LC 375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- Leetcode 375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode: Guess Number Higher or Lower II
e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...
- [Swift]LeetCode375. 猜数字大小 II | Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
随机推荐
- Android DecorView浅析
摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...
- 基于特征码文件恢复工具magicrescue
基于特征码文件恢复工具magicrescue 常见类型的文件都包含一些特殊的字节,用来标识文件的类型.这些字节被称为特征码.在磁盘中,当记录文件存储位置的簇损坏后,就可以基于这些特征码来恢复文件. ...
- 你了解border-radius吗?
1.圆角正方形 .rounded-square{ width: 200px; height: 200px; background-color: pink; border-radius: 50px; } ...
- [ 转载 ] Java中常用的设计模式
Java中常用的设计模式 1.单例模式 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建自己的唯一实例. 3.单例类必须给所有其他对象提供这一实例. 单例模式确保某个类只有一个 ...
- Redis 服务器命令
1.BGREWRITEAOF 异步执行一个 AOF(AppendOnly File) 文件重写操作 2.BGSAVE 在后台异步保存当前数据库的数据到磁盘 3.CLIENT KILL [ip:port ...
- request.get_full_path() 和request.path区别
1. 都是获取request 请求的url路径 2. request.get_full_path() -- 获取当前url,(包含参数) 请求一个http://127.0.0.1:8000/200/? ...
- python语法(一)
Python是一种面向对象.直译式电脑编程语言,也是一种功能强大的通用型语言,已经具有近二十年的发展历史,成熟且稳定.在近几年,大数据,人工智能火起来之后也是水涨船高,被越来越多的人知道,并且越来越多 ...
- Luogu P4606 [SDOI2018] 战略游戏 圆方树 虚树
https://www.luogu.org/problemnew/show/P4606 把原来的图的点双联通分量缩点(每个双联通分量建一个点,每个割点再建一个点)(用符合逻辑的方式)建一棵树(我最开始 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- 【SPFA判断负环】BZOJ1715- [Usaco2006 Dec]Wormholes 虫洞
[题目大意] 判断一张图中是否存在负环. [思路] dfs版SPFA. #include<bits/stdc++.h> using namespace std; struct edge { ...