Python | Leetcode Python题解之第375题猜数字大小II

题目:

Python | Leetcode Python题解之第375题猜数字大小II_第1张图片

题解:

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], k + max(f[i][k - 1], f[k + 1][j]))
        return f[1][n]

你可能感兴趣的:(分享,Python,Leetcode,题解)