LeetCode 题解工作台

猜数字大小 II

我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字。 你来猜我选了哪个数字。 如果你猜到正确的数字,就会 赢得游戏 。 如果你猜错了,那么我会告诉你,我选的数字比你的 更大或者更小 ,并且你需要继续猜数。 每当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。如…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们定义 表示在区间 $[i, j]$ 中猜中任意一个数最少需要花费的钱数。初始时 $f[i][i] = 0$,因为猜中了唯一的数不需要花费,对于 $i \gt j$ 的情况,也有 $f[i][j] = 0$。答案即为 。 对于 ,我们可以枚举 $[i, j]$ 中的任意一个数 ,将区间 $[i, j]$ 分为 $[i, k - 1]$ 和 $[k + 1, j]$ 两部分,选择其中的较大值加上…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

我们正在玩一个猜数游戏,游戏规则如下:

  1. 我从 1 n 之间选择一个数字。
  2. 你来猜我选了哪个数字。
  3. 如果你猜到正确的数字,就会 赢得游戏
  4. 如果你猜错了,那么我会告诉你,我选的数字比你的 更大或者更小 ,并且你需要继续猜数。
  5. 每当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。如果你花光了钱,就会 输掉游戏

给你一个特定的数字 n ,返回能够 确保你获胜 的最小现金数,不管我选择那个数字

 

示例 1:

输入:n = 10
输出:16
解释:制胜策略如下:
- 数字范围是 [1,10] 。你先猜测数字为 7 。
    - 如果这是我选中的数字,你的总费用为 $0 。否则,你需要支付 $7 。
    - 如果我的数字更大,则下一步需要猜测的数字范围是 [8,10] 。你可以猜测数字为 9 。
        - 如果这是我选中的数字,你的总费用为 $7 。否则,你需要支付 $9 。
        - 如果我的数字更大,那么这个数字一定是 10 。你猜测数字为 10 并赢得游戏,总费用为 $7 + $9 = $16 。
        - 如果我的数字更小,那么这个数字一定是 8 。你猜测数字为 8 并赢得游戏,总费用为 $7 + $9 = $16 。
    - 如果我的数字更小,则下一步需要猜测的数字范围是 [1,6] 。你可以猜测数字为 3 。
        - 如果这是我选中的数字,你的总费用为 $7 。否则,你需要支付 $3 。
        - 如果我的数字更大,则下一步需要猜测的数字范围是 [4,6] 。你可以猜测数字为 5 。
            - 如果这是我选中的数字,你的总费用为 $7 + $3 = $10 。否则,你需要支付 $5 。
            - 如果我的数字更大,那么这个数字一定是 6 。你猜测数字为 6 并赢得游戏,总费用为 $7 + $3 + $5 = $15 。
            - 如果我的数字更小,那么这个数字一定是 4 。你猜测数字为 4 并赢得游戏,总费用为 $7 + $3 + $5 = $15 。
        - 如果我的数字更小,则下一步需要猜测的数字范围是 [1,2] 。你可以猜测数字为 1 。
            - 如果这是我选中的数字,你的总费用为 $7 + $3 = $10 。否则,你需要支付 $1 。
            - 如果我的数字更大,那么这个数字一定是 2 。你猜测数字为 2 并赢得游戏,总费用为 $7 + $3 + $1 = $11 。
在最糟糕的情况下,你需要支付 $16 。因此,你只需要 $16 就可以确保自己赢得游戏。

示例 2:

输入:n = 1
输出:0
解释:只有一个可能的数字,所以你可以直接猜 1 并赢得游戏,无需支付任何费用。

示例 3:

输入:n = 2
输出:1
解释:有两个可能的数字 1 和 2 。
- 你可以先猜 1 。
    - 如果这是我选中的数字,你的总费用为 $0 。否则,你需要支付 $1 。
    - 如果我的数字更大,那么这个数字一定是 2 。你猜测数字为 2 并赢得游戏,总费用为 $1 。
最糟糕的情况下,你需要支付 $1 。

 

提示:

  • 1 <= n <= 200
lightbulb

解题思路

方法一:动态规划

我们定义 f[i][j]f[i][j] 表示在区间 [i,j][i, j] 中猜中任意一个数最少需要花费的钱数。初始时 f[i][i]=0f[i][i] = 0,因为猜中了唯一的数不需要花费,对于 i>ji \gt j 的情况,也有 f[i][j]=0f[i][j] = 0。答案即为 f[1][n]f[1][n]

对于 f[i][j]f[i][j],我们可以枚举 [i,j][i, j] 中的任意一个数 kk,将区间 [i,j][i, j] 分为 [i,k1][i, k - 1][k+1,j][k + 1, j] 两部分,选择其中的较大值加上 kk 的花费,即 max(f[i][k1],f[k+1][j])+k\max(f[i][k - 1], f[k + 1][j]) + k 的最小值。

时间复杂度 O(n3)O(n^3),空间复杂度 O(n2)O(n^2)。其中 nn 为猜测的数字范围。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def getMoneyAmount(self, n: int) -> int:
        f = [[0] * (n + 1) for _ in range(n + 1)]
        for i in range(n - 1, 0, -1):
            for j in range(i + 1, n + 1):
                f[i][j] = j + f[i][j - 1]
                for k in range(i, j):
                    f[i][j] = min(f[i][j], max(f[i][k - 1], f[k + 1][j]) + k)
        return f[1][n]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate explain how the dynamic programming approach minimizes the worst-case cost?

  • question_mark

    Does the candidate understand how to construct subproblems for a given range and transition between them?

  • question_mark

    Can the candidate identify the optimal substructure and explain how it applies to this problem?

warning

常见陷阱

外企场景
  • error

    Choosing the wrong number to guess within a range, leading to an inefficient solution with higher costs.

  • error

    Failing to recognize the optimal substructure and trying to brute force the problem instead of using dynamic programming.

  • error

    Not properly accounting for all possible subranges and making incorrect assumptions about the problem's state transitions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the range starts at a number other than 1?

  • arrow_right_alt

    What if the cost function changes and isn't directly tied to the guess number?

  • arrow_right_alt

    How does this problem change if you're allowed to make multiple guesses instead of just one?

help

常见问题

外企场景

猜数字大小 II题解:状态·转移·动态规划 | LeetCode #375 中等