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 ...
随机推荐
- linux下更换pip源
pip不更换源的话,速度可能非常慢.这里将pip源更换为阿里云源. 1.修改文件~/.pip/pip.conf(没有该文件则创建一个) $ sudo vim ~/.pip/pip.conf 2.写入以 ...
- 匹配合法IP地址的正则表达式
IPV4地址分为4段,以点号分隔.如192.168.26.13.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配: 第一步:地址分析,正则初判 0-9 \d 进行匹配 1 ...
- CSU - 2062 Z‘s Array
Description Z likes to play with array. One day his teacher gave him an array of n elements, and ask ...
- <泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...
- modernizr.js的介绍和使用
原文链接:https://www.cnblogs.com/-simon/p/5907053.html Modernizr帮助我们检测浏览器是否实现了某个feature,如果实现了那么开发人员就可以充分 ...
- 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测
使用python3 学习了线性回归的api 分别使用逻辑斯蒂回归 和 随机参数估计回归 对良恶性肿瘤进行预测 我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://gith ...
- 使用SoapUI测试windows身份验证的webservice
有个朋友问到用soapui测试wcf服务时如果使用windows身份验证要怎么传输凭据,于是自己试了一下.其实服务端是wcf还是webservice还是webapi都无所谓,关键是windows身份验 ...
- 「WC2016」挑战NPC
「WC2016」挑战NPC 解题思路 这个题建图非常厉害,带花树什么的只会口胡根本写不动,所以我写了机房某大佬教我的乱搞. 考虑把一个筐 \(x\) 拆成 \(x1,x2,x3\) 三个点,且这三个点 ...
- 通过Nuget添加Mvvmlight框架发生错误
IDE:Visual Studio 2013 场景:通过Nuget添加Mvvmlight框架 具体错误: 解决办法:删除Nuget,然后添加新版本的Nuget Package Manager 具体操作 ...
- numpy中的random函数
1:rand rand(d0, d1, ..., dn) Random values in a given shape. Create an array of the given shap ...