leetcode Guess Number Higher or Lower II

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 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:

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 the first 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?

题目地址: leetcode Guess Number Higher or Lower II

题意:给定一个数字n,从1~n中选取一个数字y让你猜。在猜的过程中,会告诉你你当前猜的数字x是比y大还是小,猜错的话,需要付$x。给定n,我们最少需要多少的钱来确保我们猜出y呢?

思路:在 374 Guess Number Higher or Lower 中,我们采用二分法来进行查找,但这题并不是用二分法。

这题要求我们在猜测数字y未知的情况下(1~n任意一个数),要我们在最坏情况下我们支付最少的钱。也就是说要考虑所有y的情况。

我们假定选择了一个错误的数x,(1<=x<=n && x!=y )那么就知道接下来应该从[1,x-1 ] 或者[x+1,n]中进行查找。 假如我们已经解决了[1,x-1] 和 [x+1,n]计算问题,我们将其表示为solve(L,x-1) 和solve(x+1,n),那么我们应该选择max(solve(L,x-1),solve(x+1,n)) 这样就是求最坏情况下的损失。总的损失就是 f(x) = x + max(solve(L,x-1),solve(x+1,n))

那么将x从1~n进行遍历,取使得 f(x) 达到最小,来确定最坏情况下最小的损失,也就是我们初始应该选择哪个数。

上面的说法其实是一个自顶向下的过程(Top-down),可以用递归来解决。很容易得到如下的代码(这里用了记忆化搜索):

C++

Java

Python

 

打印路径

我们可以将路径打印出来,来看看具体的选择策略的过程,以便加深理解:

比如n = 10的时候, 上述程序输出7,9 说明第一步选7第二步选9这是最坏的情况。

有没有更坏的情况呢?答案是没有。7的右半边[8,10]最坏情况就是9了。7的左半边[1,6]可以选个3,然后选个5达到最坏的情况。3+5=8<9

 

迭代的方法

在记忆化搜索的解法中,c++ 88ms, java 15ms, 而Python则高达1800ms,差点TLE,确实效率不高。

于是将其改为迭代如下:

C++ 56ms

Java 14ms

Python 576ms

 

 

 

本题是leetcode 375 Guess Number Higher or Lower II 题解

更多题解可以查看:https://www.hrwhisper.me/leetcode-algorithm-solution/

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Guess Number Higher or Lower II
本文地址:https://www.hrwhisper.me/leetcode-guess-number-higher-lower-ii/

听说帅的人已经打赏了

Leetcode , , , , , . permalink.

12 thoughts on “leetcode Guess Number Higher or Lower II

  1. 我把c++迭代版本中的vector换成了int数组,运行时间是28ms,几乎是Java版本(15ms)的两倍。根本原因是什么?

Leave a Reply

Your email address will not be published. Required fields are marked *