Guess number higher or Lower ii--confused

Source: Internet
Author: User

Today, try to do a leetcode OJ above the No. 375 problem: Guess number higher or Lower II

Original title Link: https://leetcode.com/problems/guess-number-higher-or-lower-ii/

The specific topics are as follows:

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.

Search the Internet, learn to use the idea of dynamic planning to solve.

At this point, there are two ways of thinking (guess the number range: 1-n):

DP[P][Q] Indicates the range of guesses between p-q to ensure the minimum cost required to win. This is titled: Dp[1][n].

Idea One:

Suppose we randomly guessed the number of K,

Dp[1][n] = k + Max{dp[1][k-1],dp[k+1][n]},

Thus, finally, a value is obtained, which is the minimum cost (maximum cost) required to win the 1-n interval guess. )。

In this process, we are constantly looking for the maximum cost of each interval, so that it will not cause the number of guesses has been changing, it is felt that the maximum is too big, and there is no point.

Silently feel is oneself think wrong, the number that guess is not changing?

Another question is: assuming that the number of guesses is the same, then if you always take the maximum cost of the sub-range, then where is the smallest embodiment?

Idea two:

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) the number guaranteed to win the money should be spent, then we go through 1-n as Finding the minimum value is the answer, which is the minimum maximum value problem.

Guess number higher or Lower ii--confused

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.