375. Guess Number Higher or Lower

来源:互联网 发布:阿里云客服好干吗 编辑:程序博客网 时间:2024/05/24 01:33

374. Guess Number Higher or Lower

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 is higher or lower.

You call a pre-defined API guess(int num) which returns 3 possible results (-11, or 0):

-1 : My number is lower 1 : My number is higher 0 : Congrats! You got it!

Example:

n = 10, I pick 6.Return 6.

// Forward declaration of guess API.// @param num, your guess// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0int guess(int num);class Solution {public:    int guessNumber(int n) {        int left=1,right=n;        int res;        while(left<=right){            int mid=left+(right-left)/2;            if(guess(mid)==0){                res=mid;                break;            }            else if(guess(mid)==-1){                right=mid-1;            }else{                left=mid+1;            }        }        return res;    }};
其实就是二分查找

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

这题要求我们在猜测数字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) 达到最小,来确定最坏情况下最小的损失


class Solution {public:int getMoneyAmount(int n) {vector<vector<int> >dp(n+1,vector<int>(n+1,0));dfs(dp,1,n);return dp[1][n];}private:int dfs(vector<vector<int> >&dp,int l,int r){if(l>=r) return 0;if(dp[l][r]) return dp[l][r];int res=INT_MAX;for(int x=l;x<=r;x++){int tmp=x+max(dfs(dp,l,x-1),dfs(dp,x+1,r));res=min(res,tmp);}dp[l][r]=res;return res;}};



0 0
原创粉丝点击