375. Guess Number Higher or Lower II Medium
1/**
2 * [375] Guess Number Higher or Lower II
3 *
4 * We are playing the Guessing Game. The game will work as follows:
5 * <ol>
6 * I pick a number between 1 and n.
7 * You guess a number.
8 * If you guess the right number, you win the game.
9 * If you guess the wrong number, then I will tell you whether the number I picked is higher or lower, and you will continue guessing.
10 * Every time you guess a wrong number x, you will pay x dollars. If you run out of money, you lose the game.
11 * </ol>
12 * Given a particular n, return the minimum amount of money you need to guarantee a win regardless of what number I pick.
13 *
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/graph.png" style="width: 505px; height: 388px;" />
16 * Input: n = 10
17 * Output: 16
18 * Explanation: The winning strategy is as follows:
19 * - The range is [1,10]. Guess 7.
20 * - If this is my number, your total is $0. Otherwise, you pay $7.
21 * - If my number is higher, the range is [8,10]. Guess 9.
22 * - If this is my number, your total is $7. Otherwise, you pay $9.
23 * - If my number is higher, it must be 10. Guess 10. Your total is $7 + $9 = $16.
24 * - If my number is lower, it must be 8. Guess 8. Your total is $7 + $9 = $16.
25 * - If my number is lower, the range is [1,6]. Guess 3.
26 * - If this is my number, your total is $7. Otherwise, you pay $3.
27 * - If my number is higher, the range is [4,6]. Guess 5.
28 * - If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $5.
29 * - If my number is higher, it must be 6. Guess 6. Your total is $7 + $3 + $5 = $15.
30 * - If my number is lower, it must be 4. Guess 4. Your total is $7 + $3 + $5 = $15.
31 * - If my number is lower, the range is [1,2]. Guess 1.
32 * - If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $1.
33 * - If my number is higher, it must be 2. Guess 2. Your total is $7 + $3 + $1 = $11.
34 * The worst case in all these scenarios is that you pay $16. Hence, you only need $16 to guarantee a win.
35 *
36 * Example 2:
37 *
38 * Input: n = 1
39 * Output: 0
40 * Explanation: There is only one possible number, so you can guess 1 and not have to pay anything.
41 *
42 * Example 3:
43 *
44 * Input: n = 2
45 * Output: 1
46 * Explanation: There are two possible numbers, 1 and 2.
47 * - Guess 1.
48 * - If this is my number, your total is $0. Otherwise, you pay $1.
49 * - If my number is higher, it must be 2. Guess 2. Your total is $1.
50 * The worst case is that you pay $1.
51 *
52 *
53 * Constraints:
54 *
55 * 1 <= n <= 200
56 *
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/guess-number-higher-or-lower-ii/
61// discuss: https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65impl Solution {
66 pub fn get_money_amount(n: i32) -> i32 {
67 0
68 }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_375() {
79 }
80}
81
Back
© 2025 bowen.ge All Rights Reserved.