Guess Number Higher or Lower II -- LeetCode
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.
However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
Example:
n = , I pick . First round: You guess , I tell you that it's higher. You pay $5.
Second round: You guess , I tell you that it's higher. You pay $7.
Third round: You guess , I tell you that it's lower. You pay $9. Game over. is the number I picked. You end up paying $ + $ + $ = $.
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.
思路:DP。
设OPT(i, j)表示i到j范围内的最优解。假设我们猜的数字是k,则i <= k <= j。之后,k将该问题分解为两个子问题,OPT(i, k-1)和OPT(k+1, j)。我们要的是其中的最坏情况。因此OPT(i, j) = min(k + max(OPT(i, k-1), OPT(k+1, j)))
复杂度为O(n^3).
class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int> > dp(n + , vector<int>(n + , ));
for (int st = n; st > ; st--) {
for (int ed = st + ; ed <= n; ed++) {
int localMin = INT_MAX;
for (int k = st; k <= ed; k++) {
int cur = k + std::max(k == st ? : dp[st][k-], k == ed ? : dp[k+][ed]);
localMin = std::min(cur, localMin);
}
dp[st][ed] = localMin;
}
}
return dp[][n];
}
};
Guess Number Higher or Lower II -- LeetCode的更多相关文章
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 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] 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: 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 ...
- 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 ...
随机推荐
- Python学习-前台开发-JavaScript、Dom和jQuery
JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...
- python /usr/bin/python^M: bad interpreter: No such file
今天在WingIDE下写了个脚本,传到服务器执行后提示: -bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No suc ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 P: 【数组】1234方阵(phalanx)
http://acm.ocrosoft.com/problem.php?cid=1316&pid=15 题目描述 编程打印如下规律的n*n方阵.输入n,按规律输出方阵. 方阵规律如下图:使左对 ...
- gulp入门1
1. 下载.安装git(https://git-scm.com/downloads),学会使用命令行. 2. 下载.安装node.js(https://nodejs.org/en/),现在node.j ...
- gulp (转)
“1. 我为什么使用grunt: 2. 我为何放弃grunt转投gulp: 3. 我为何放弃gulp与grunt,转投npm scripts: 4. 我为何放弃前端” —— 司徒正美 前端(段子)界的 ...
- java链接数据库--Mysql
/************************************************************************* > File Name: Mysql.jav ...
- 如何在CentOS7上改变网络接口名
如何在CentOS7上改变网络接口名 传统上,Linux的网络接口被枚举为eth[0123...],但这些名称并不一定符合实际的硬件插槽,PCI位置,USB接口数量等,这引入了一个不可预知的命名问题( ...
- [洛谷P4725]【模板】多项式对数函数
题目大意:给出$n-1$次多项式$A(x)$,求一个 $\bmod{x^n}$下的多项式$B(x)$,满足$B(x) \equiv \ln A(x)$.在$\bmod{998244353}$下进行.保 ...
- POJ 1149 PIGS | 最大流问题
参考了这个PDF 第一道网络流啊!感动 #include<cstdio> #include<algorithm> #include<cstring> #includ ...