Transmission Door
375. Guess number higher or Lower IIQuestionEditorial SolutionMy Submissions
- Total accepted:1546
- Total submissions:5408
- Difficulty:medium
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to N. You had 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 your guess wrong, you pay $x. You win the game when you guess the number I picked.
Example:
n = ten, I pick 8.First round: You guess 5, I tell you it's higher. You pay $5.second round:you guess 7 and I tell you it's higher. You pay $7.third round: Your guess 9, I tell you it ' s lower. You pay $9.game. 8 is the number I picked. You end up paying $ $7 + $9 = $21.
Given a particular n≥1, find out what much money you need to has to guarantee a win
Turn from:
http://blog.csdn.net/adfsss/article/details/51951658
The main idea or guess the game, but this time the focus of change, the topic requires you guessed wrong to pay you guessed the amount of money, the final should be found to ensure that you can win the amount (that is, to find out the minimum money to ensure that you win), the topic of the tip is dynamic planning, self-eating is not very good, Think of the previous dynamic planning is starting from the initial conditions, and then roll out the values, so the Cindy managed Cindy managed to write the previous several situations in the law, forget, found Max (the last four of the amount of money to be judged, the bottom fourth bit of money + front of the money) seems quite possible, But the submission of the time found that N is more than 19 o'clock can not pass, so decisively rejected their ideas, because the idea of finding the law can not convince themselves why this is the minimum value, here also give themselves knocked a wake-up call, do not try to use the psychological to deceive themselves, online search after the Found not to speak of the more clear blog, all are posted code (っ*´д ') our store (understand not to feed), behind in the Leetcode of the discuss inside there are two sentences to understand the English.
Specifically, in the number of 1-n, we randomly guess a number (set to i), to ensure that the money spent on winning should be i + MAX (W (1, I-1), W (i+1, N)), here W (x, y) to guess the range in (x, y) number guaranteed to win the money should be spent, then we loop 1 N as the number of guesses, find the minimum value is the answer, that is, the minimum maximum value problem
Turn:
C + + vector multidimensional array initialization and zeroing
http://blog.csdn.net/xiaxiazls/article/details/50018225
define and initialize a two-dimensional array vector<vector <int> > Ivec (M,vector<int> (n,0/ /m*n Two-dimensional vector, all elements initialized to 0
1 classSolution {2 Public:3 intGetmoneyamount (intN) {4 //vector<vector <int> > Ivec (M,vector<int> (n,0));//M*n's two-dimensional vector, all elements initialized to 05Vector<vector <int> > DP (n +1,vector<int> (n +1, Int_max));//N*n's two-dimensional vector, all elements initialized to Int_max6DFS (DP,1, n);7 returndp[1][n];8 }9 intDFS (Vector<vector <int> > &DP,intLintR) {Ten if(Dp[l][r]! =Int_max) { One returnDp[l][r]; A } - if(L = =R) { - returnDP[L][R] =0; the } - if(L +1==R) { - returnDP[L][R] =l; - } + inti; - for(i = l +1; I <= R-1; i++){ +Dp[l][r] = min (dp[l][r],i + max (DFS (Dp,l,i-1), DFS (Dp,i +1, R)) ); A } at returnDp[l][r]; - } -};
Leetcode 375. Guess number higher or Lower II