375 Guess Number Higher or Lower II 猜数字大小 II

我们正在玩一个猜数游戏,游戏规则如下:
我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字。
每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了。
然而,当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。直到你猜到我选的数字,你才算赢得了这个游戏。
示例:
n = 10, 我选择了8.
第一轮: 你猜我选择的数字是5,我会告诉你,我的数字更大一些,然后你需要支付5块。
第二轮: 你猜是7,我告诉你,我的数字更大一些,你支付7块。
第三轮: 你猜是9,我告诉你,我的数字更小一些,你支付9块。
游戏结束。8 就是我选的数字。
你最终要支付 5 + 7 + 9 = 21 块钱。
给定一个 n ≥ 1,计算你至少需要拥有多少现金才能确保你能赢得这个游戏。

详见:https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/

C++:

class Solution {
public:
    int getMoneyAmount(int n)
    {
        vector<vector<int>> memo(n + 1, vector<int>(n + 1, 0));
        return helper(1, n, memo);
    }
    int helper(int start, int end, vector<vector<int>>& memo)
    {
        if (start >= end)
        {
            return 0;
        }
        if (memo[start][end] > 0)
        {
            return memo[start][end];
        }
        int res = INT_MAX;
        for (int k = start; k <= end; ++k)
        {
            int t = k + max(helper(start, k - 1, memo), helper(k + 1, end, memo));
            res = min(res, t);
        }
        return memo[start][end] = res;
    }
};

参考:http://www.cnblogs.com/grandyang/p/5677550.html

https://blog.csdn.net/adfsss/article/details/51951658

原文地址:https://www.cnblogs.com/xidian2014/p/8848366.html

时间: 2024-08-29 06:31:04

375 Guess Number Higher or Lower II 猜数字大小 II的相关文章

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My 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 have to guess w

375. Guess Number Higher or Lower II (Python)

375. Guess Number Higher or Lower II Description We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is hig

LC 375. Guess Number Higher or Lower II

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have 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 particula

375. Guess Number Higher or Lower II

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have 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 particula

[LeetCode] Guess Number Higher or Lower II 猜数字大小之二

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have 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 particula

LeetCode Guess Number Higher or Lower II

原题链接在这里:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether

Guess Number Higher or Lower II--困惑

今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 具体题目如下: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which numb

374. Guess Number Higher or Lower

/* * 374. Guess Number Higher or Lower * 2016-7-17 by Mingyang */ public int guessNumber(int n) { if (guess(n) == 0) return n; int left = 1, right = n; while (left < right) { int mid = left + (right - left) / 2, t = guess(mid); if (t == 0) return mid

猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了, 如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)

产生0-100之间的随机数,包括0和100 double d = Math.random() * 100; int a = (int)Math.round(d); ------------------------------------- package 水仙花数; import java.util.Scanner;/* 猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了,如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)*/public class t