今天,试着做了一下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 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 you guess wrong, you pay $x. You win the game when you guess the number I picked.

Example:

n = 10, I pick 8.

First round:  You guess 5, I tell you that it's higher. You pay $5.
Second round: You guess 7, I tell you that it's higher. You pay $7.
Third round: You guess 9, I tell you that it's lower. You pay $9. Game over. 8 is the number I picked. You end up paying $5 + $7 + $9 = $21.

Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.

上网搜了一下,了解到用动态规划的思路解决。

这时,有两种思路(猜的数范围为:1-n):

dp[p][q]表示猜数的范围在p-q之间要保证赢需要的最小花费。此题为:dp[1][n].

思路一:

假设我们随意猜的数为k,

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

如此,最后得到了一个值,此值为在1-n区间猜数保证赢所需要的最小花费(最大花费?)。

在此过程中,我们不断的在寻求每个区间的最大花费,这样会不会导致所猜的数一直在改变,感觉这样求出的最大就太大了,而且也没有什么意义。

默默的感觉是自己想错了,所猜的数没有在变?

还有一个问题是:假设所猜的数不变,那么如果一直取子区间所需要的最大花费,那么最小又从哪里体现呢?

思路二:

在1-n个数里面,我们任意猜一个数(设为i),保证获胜所花的钱应该为 i + max(w(1 ,i-1), w(i+1 ,n)),这里w(x,y))表示猜范围在(x,y)的数保证能赢应花的钱,则我们依次遍历 1-n作为猜的数,求出其中的最小值即为答案,即最小的最大值问题。

Guess Number Higher or Lower II--困惑的更多相关文章

  1. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  2. 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 gues ...

  3. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. [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 gues ...

  5. [LeetCode] 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 gues ...

  6. [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  7. leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II

    374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...

  8. Leetcode 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 gues ...

  9. Leetcode: Guess Number Higher or Lower II

    e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...

  10. [Swift]LeetCode375. 猜数字大小 II | 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 gues ...

随机推荐

  1. SQLServer解析xml到Oracle

    写了一个程序:根据状态位读取SQLserver 中的一张表,下载其中一个字段的值,这个值是XML类型的,然后把这个XML文件的内容插入到另一Oracle数据库,并更新SQLServer表的标志位,表示 ...

  2. ajax 异步插入图片到数据库(多图上传)

    额 大概就这么个样子...截个图 点浏览  选择几张图片 选择完了 确定一下 然后插入数据库 同时在页面中显示插入的图片,代码 也没啥.看下 index.php <html><hea ...

  3. Asp.net MVC3表格共用分页功能

    在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页. 第一 ...

  4. Mac +WebStorm+nodeJs+Freemarker.js的安装与使用

    第一步用webStorm新建node+express项目 第二步执行npm i –save freemarker.js,安装 freemarker.js 模块 第三步安装java jdk包 jdk包地 ...

  5. discuz手机版模板开发

    1.触屏版模板手机路径 discuz X3触屏版模板路径:/template/default/touch/forum/discuz.htm(主页面模板) discuz X3标准版模板路径:/templ ...

  6. Atom使用到的插件

    atom-beautify atom-css-class-checker atom-css-comb atom-css-unit-converter atom-csscomb atom-csslint ...

  7. hibernate5ID生成策略

    1.uuid2:使用JDK自带的UUID生成36位的ID 2.guid: 3.uuid:生成32位的uuid,不符合ETF RFC 4122标准,已被uuid2取代. 4.uuid.hex:等同uui ...

  8. 破解Google Gmail的https新思路

    最近,Google针对gmail被攻击事件,全面默认启用了始终以https访问Gmail的方式了.但是,对于可以动用整个国家力量的黑客来说,从网络通讯数据中(在此不讨论对用户电脑种木马破解https的 ...

  9. VS2013 有效密钥

    今天打开笔记本上的VS2013,发现试用版到期了,就到网上找密钥,找了一些时候找到一个有效序列号,记录如下: BWG7X-J98B3-W34RT-33B3R-JVYW9

  10. wpf初步-grid布局-连连看棋盘

    private void Window_Loaded_1(object sender, RoutedEventArgs e) { //Button btn1 = new Button(); //btn ...