Sunday, August 21, 2016

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 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?
The question asks us to find the minimum money we need to pay in the worst scenario. The hint suggests its a minimax problem. I think try to understand the whole algorithm is too complicated. Let's explain it in an easy way. Given range [lower, upper] and a number y, we want to find the minimum money we need to pay before we make the correct guess.For a wrong guess x != y, we know the  next guess lies in [lower, x - 1] or [x + 1, upper], the worst scenario exists in the maximum of the money to pay between these two ranges. So the minimum money we need to pay in range [lower, upper] is:

Math.min(money[lower][upper], Math.max(solve(lower, x - 1), solve(x + 1, upper))) for x in [lower, upper], inclusive. 


    public int getMoneyAmount(int n) {
        int[][] money = new int[n + 1][n + 1];
        return solve(money, 1, n);
    }
    
    public int solve(int[][] money, int lower, int upper) {
        if (lower >= upper) {
            return 0;
        }
        if (money[lower][upper] != 0) {
            return money[lower][upper];
        }
        money[lower][upper] = Integer.MAX_VALUE;
        for (int i = lower; i <= upper; i++) {
            money[lower][upper] = Math.min(money[lower][upper], 
            Math.max(solve(money, lower, i - 1), solve(money, i + 1, upper)) + i);
        }
        return money[lower][upper];
    }


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete