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 = 10, I pick 8.

First round:  You guess 5, I tell you that it's higher. You pay $5.
Second round: You guess 7, I tell you that it's higher. You pay $7.
Third round: You guess 9, I tell you that it's lower. You pay $9. Game over. 8 is the number I picked. You end up paying $5 + $7 + $9 = $21.

Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.

Hint:

  1. The best strategy to play the game is to minimize the maximum loss you could possibly face. Another strategy is to minimize the expected loss. Here, we are interested in thefirst scenario.
  2. Take a small example (n = 3). What do you end up paying in the worst case?
  3. Check out this article if you're still stuck.
  4. The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming.
  5. As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss?

Credits:
Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases.

此题是之前那道 Guess Number Higher or Lower 的拓展,难度增加了不少,根据题目中的提示,这道题需要用到 Minimax 极小化极大算法,关于这个算法可以参见这篇讲解,并且题目中还说明了要用 DP 来做,需要建立一个二维的 dp 数组,其中 dp[i][j] 表示从数字i到j之间猜中任意一个数字最少需要花费的钱数,那么需要遍历每一段区间 [j, i],维护一个全局最小值 global_min 变量,然后遍历该区间中的每一个数字,计算局部最大值 local_max = k + max(dp[j][k - 1], dp[k + 1][i]),这个正好是将该区间在每一个位置都分为两段,然后取当前位置的花费加上左右两段中较大的花费之和为局部最大值,为啥要取两者之间的较大值呢,因为要 cover 所有的情况,就得取最坏的情况。然后更新全局最小值,最后在更新 dp[j][i] 的时候看j和i是否是相邻的,相邻的话赋为j,否则赋为 global_min。这里为啥又要取较小值呢,因为 dp 数组是求的 [j, i] 范围中的最低 cost,比如只有两个数字1和2,那么肯定是猜1的 cost 低,是不有点晕,没关系,博主继续来绕你。如果只有一个数字,那么不用猜,cost 为0。如果有两个数字,比如1和2,猜1,即使不对,cost 也比猜2要低。如果有三个数字 1,2,3,那么就先猜2,根据对方的反馈,就可以确定正确的数字,所以 cost 最低为2。如果有四个数字 1,2,3,4,那么情况就有点复杂了,策略是用k来遍历所有的数字,然后再根据k分成的左右两个区间,取其中的较大 cost 加上k。

当k为1时,左区间为空,所以 cost 为0,而右区间 2,3,4,根据之前的分析应该取3,所以整个 cost 就是 1+3=4。

当k为2时,左区间为1,cost 为0,右区间为 3,4,cost 为3,整个 cost 就是 2+3=5。

当k为3时,左区间为 1,2,cost 为1,右区间为4,cost 为0,整个 cost 就是 3+1=4。

当k为4时,左区间 1,2,3,cost 为2,右区间为空,cost 为0,整个 cost 就是 4+2=6。

综上k的所有情况,此时应该取整体 cost 最小的,即4,为最后的答案,这就是极小化极大算法,参见代码如下:

解法一:

class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int>> dp(n + , vector<int>(n + , ));
for (int i = ; i <= n; ++i) {
for (int j = i - ; j > ; --j) {
int global_min = INT_MAX;
for (int k = j + ; k < i; ++k) {
int local_max = k + max(dp[j][k - ], dp[k + ][i]);
global_min = min(global_min, local_max);
}
dp[j][i] = j + == i ? j : global_min;
}
}
return dp[][n];
}
};

下面这种是递归解法,建立了记忆数组 memo,减少了重复计算,提高了运行效率,核心思想跟上面的解法相同,参见代码如下:

解法二:

class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int>> memo(n + , vector<int>(n + , ));
return helper(, n, memo);
}
int helper(int start, int end, vector<vector<int>>& memo) {
if (start >= end) return ;
if (memo[start][end] > ) return memo[start][end];
int res = INT_MAX;
for (int k = start; k <= end; ++k) {
int t = k + max(helper(start, k - , memo), helper(k + , end, memo));
res = min(res, t);
}
return memo[start][end] = res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/375

类似题目:

Guess Number Higher or Lower

Flip Game II

Can I Win

Find K Closest Elements

参考资料:

https://leetcode.com/problems/guess-number-higher-or-lower-ii/

https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/84787/Java-DP-solution

https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/84764/Simple-DP-solution-with-explanation~~

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 375. Guess Number Higher or Lower II 猜数字大小之二的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 375 Guess Number Higher or Lower II 猜数字大小 II

    我们正在玩一个猜数游戏,游戏规则如下:我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字.每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了.然而,当你猜了数字 x 并且猜错了的时候,你需 ...

  4. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  5. 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 ...

  6. [leetcode]375 Guess Number Higher or Lower II (Medium)

    原题 思路: miniMax+DP dp[i][j]保存在i到j范围内,猜中这个数字需要花费的最少 money. "至少需要的花费",就要我们 "做最坏的打算,尽最大的努 ...

  7. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 多维度对比5款主流分布式MQ消息队列,妈妈再也不担心我的技术选型了

    1.引言 对于即时通讯网来说,所有的技术文章和资料都在围绕即时通讯这个技术方向进行整理和分享,这一次也不例外.对于即时通讯系统(包括IM.消息推送系统等)来说,MQ消息中件间是非常常见的基础软件,但市 ...

  2. 【转】Visual Studio 2008 可扩展性开发(二):Macro和Add-In初探

    前言 在VS概览中,我们简单回顾了一下VS的历史.本文将通过两个简单的例子来说明Macro和Add-In的开发.通过Macro我们把VS中的一些重复操作录制下来,之后可以多次运行,节省时间并保持好的心 ...

  3. Prometheus 监控K8S集群资源监控

    Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...

  4. SQL Server温故系列(0):导航目录

    创建本系列博文通用库表及数据的 SQL 语句:下载 SQL Server温故系列(0):导航目录 SQL Server温故系列(1):SQL 数据操作 CRUD 之增删改合 SQL Server温故系 ...

  5. git 使用 tortoisegit 解冲突

    git 解冲突需要注意的问题 弄清除冲突双向的修改意图,并在解决冲突时,同时处理两边的意图. 举例说明 A.txt 文件, 在 master 分支上,有一行文字(代码)是这样: 这是一段在 maste ...

  6. 最简单取得外网IP

    #coding=utf-8import urllib2import re print re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http ...

  7. Locust 接口性能测试 - 转载一 (后期熟悉实践自己出一套完整的)

    转载大佬   ,.. 另外一篇:https://www.cnblogs.com/imyalost/p/9758189.html记录一下接口性能测试的学习 先熟悉一下概念: Locust是使用Pytho ...

  8. android studio学习----android studio断点调试

    先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指 ...

  9. Arthas实践--抽丝剥茧排查线上应用日志打满问题

    现象 在应用的 service_stdout.log里一直输出下面的日志,直接把磁盘打满了: 23:07:34.441 [TAIRCLIENT-1-thread-1] DEBUG io.netty.c ...

  10. [b0009] 玩Hadoop中碰到的各种错误

    1. Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class mp.filetest.WordCount2 ...