ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

375. Guess Number Higher or Lower II

2019-02-17 23:42:14  阅读:202  来源: 互联网

标签:Lower return int pay Guess guess Number number dp


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.

 

Approach #1: DP. [C++]

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

 

Analysis:

dp[i][j] : the index from i to j how much money you have to pay.

res = i + max(fuc(), fuc());

the reasion why we use max instead of min is that we want to find the minimum money which we have to pay. If we use min in this place it will return 0 alway.

 

Approach #2: bottom to up. [C++]

    int getMoneyAmount(int n) {
        vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
        for (int i = 2; i <= n; ++i) {
            for (int j = i-1; j > 0; --j) {
                int globalMin = INT_MAX;
                for (int k = j+1; k < i; ++k) {
                    int localMin = k + max(dp[j][k-1], dp[k+1][i]);
                    globalMin = min(globalMin, localMin);
                }
                dp[j][i] = j+1 == i ? j : globalMin;
            }
        }
        return dp[1][n];
    }

  

Analysis:

If j+1 == i, dp[j][i] represent two adjacent numbers, so we return the little one as the value of dp[j][i]. why not is the bigger one? I think it is realted to we solve this problem using bottom to up method. so we should get the little one's value firstly.

 

标签:Lower,return,int,pay,Guess,guess,Number,number,dp
来源: https://www.cnblogs.com/ruruozhenhao/p/10393272.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有