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 ...
随机推荐
- Oracle 自增写给自己的
首先咱先建一张表: CREATE TABLE example( ID Number(4) NOT NULL PRIMARY KEY, NAME VARCHAR(25), PHONE VARCHAR(1 ...
- python 学习分享-select等
首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组(在linux中 ...
- PAT——甲级1042:Shuffling Mashine
终于做到甲级了 就一个感觉....题目是真的看不懂,亏我还是四六级都过了的人....可是看完题愣是一点都不懂是什么意思. 1042 Shuffling Machine (20 point(s)) Sh ...
- django的聚合函数和aggregate、annotate方法使用
支持聚合函数的方法: 提到聚合函数,首先我们要知道的就是这些聚合函数是不能在django中单独使用的,要想在django中使用这些聚合函数,就必须把这些聚合函数放到支持他们的方法内去执行.支持聚合函数 ...
- shell中的>&1和 >&2是什么意思?
当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...
- npm & npm config
npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...
- Scala 基础(5)—— 构建函数式对象
有了 Scala 基础(4)—— 类和对象 的前提,现在就可以来构建一个基于 Scala 的函数式对象. 下面开始构造一个有理数对象 Rational. 1. 主构造方法和辅助构造方法 对于每一个类的 ...
- P3456 [POI2007]GRZ-Ridges and Valleys
题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是相邻的格子.(所以与(i,j)相邻的格子有(i ...
- jquery使滚动条滚动到最底部
$('body').scrollTop($('body')[0].scrollHeight); //想要加载页面自动到最底部要写入function中使用setTimeout function top1 ...
- SQL 设置自增,和default
mysql数据库为表中已有的主键字段增加自增属性: ALTER TABLE `category ` MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT ...