LintCode 666. Number Guessing Game II

We are playing a guessing game, the content of the game is as follows:
I choose a number in the range of 1 to n as the number to be guessed, you need to guess the number,
every time you guess wrong, I will tell you what I chose Whether the number is higher or lower than what you said,
however, when you guess the number is x and you guess wrong, you pay $x. When you guess the number I choose, you win the game

The example
gives n = 10, I choose the number to guess as 8
First round: you guess 5, I tell you the value to guess is bigger. You need to pay $5
Second round: you guess 7, I Tell you that the guess is bigger. You pay $7
3rd round: you guess 9, I tell you the guess is smaller. You pay $9

Game over. 8 is my chosen number to guess.
You end up paying $5 + $7 + $9 = $21

Given a specific number n greater than or equal to 1, calculate how much money you need to guarantee a win.
So when n = 10, return 16.

 

Idea: dynamic programming, this number guessing game can be promoted from small to large. For example, the size of the interval I know is 1, such as in [1,1], then I can guess the number without any effort, that is pay=0;
if the size of the number interval is 2, such as [n,n+1], we only need to guess that the number is n, and we can guess the last number according to the result, so we can guess it by paying at most pay=n Numbers;
when the interval is 3, [n,n+2], we only need to guess n+1 to be able to directly determine, at this time the payment is pay = n+1;
...
extended to the interval of size m+1 , that is, in the interval [n,n+m], the recurrence formula can be obtained: DP[n][n+m] = min(max(DP[n][x-1],DP[x+1]) + x) where n<x<m;

DP[i][j] represents the minimum effort required to guess the number in ij.
According to this recursive formula, the solution can be obtained using dynamic programming, and finally DP[1][n] is returned.

 

1  class Solution {
 2  public :
 3      /* *
 4       * @param n: An integer
 5       * @return: how much money you need to have to guarantee a win
 6       */ 
7      int getMoneyAmount( int n) {
 8          // write your code here 
9          vector<vector< int >> DP(n+ 1 ,vector< int >(n+ 1 , 0x7fffffff )); // Because there is an updated minimum value later, it is initialized to INT_MAX 
10          for ( int i =1 ; i<n; ++i) { // initialize 
11              DP[i][i+ 1 ] = i;
 12              DP[i][i] = 0 ;
 13          };
 14          DP[n][n] = 0 ;
 15          for ( int range = 2 ; range<n; ++range){ // range represents the range size 
16              for ( int i = 1 ;i<n;++ i){
 17                  if (i+range<=n ){ // Out of bounds check 
18                      for ( int j = i+ 1; j<i+range; ++ j){
 19                          DP[i][i+range] = min(DP[i][i+range],max(DP[i][j- 1 ],DP[j+ 1 ][i+range])+j); // Refer to recursion formula 
20                      }
 21                  }
 22              }
 23          }
 24          return DP[ 1 ][n];
 25      }
 26 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324550630&siteId=291194637