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 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--困惑的更多相关文章
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- 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 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- (48) odoo的button用法
button 应用的比较多,这里来汇总一下 按钮<button>,支持的属性 icon 可用的icon在 addons/web/static/src/img/icons ...
- jmeter for循环嵌套if学习1
测试计划组成: vcondition的值都是tom. 执行结果:
- Mysql 拼接字段查询语句和join查询拼接和时间查询
个人平时记录的,有点乱 1.修改时间字段,如果时间字段的类型是date或者是datetime类型的 update 表名 set 时间字段 = DATE_FORMAT(NOW(),'%Y-%m-%d % ...
- 新春测 kinect motor
大年30,祝所有开发伙伴新春快乐. 天天FQ, 叹国内学习成本太高 看到一篇台湾 kinect 电机控制, 赞 using Microsoft.Kinect; using System; using ...
- jquery.validate:
jqueryValidation: jquery-UI 小组组长;https://jqueryvalidation.org/;从页面性能的角度来说: 最好是把js的引入放在结束的body标签上面;基本 ...
- Python--逆序打印
才开始学习Python,我个人喜欢边看实例边学习其中的知识点,于是在网上找到了“Python100例”, 案例很不错,但是其中有几个例子不能正确实现,比如第29个例子--“给一个不多于5位的正整数, ...
- [整]磁盘 I/O 性能监控指标和调优方法
在介绍磁盘 I/O 监控命令前,我们需要了解磁盘 I/O 性能监控的指标,以及每个指标的所揭示的磁盘某方面的性能. 磁盘 I/O 性能监控的指标主要包括: 指标 1:每秒 I/O 数(IOPS 或 t ...
- Struts2 Action下面的Method调用方法
1. 在struts.xml中加入<constant name="struts.enable.DynamicMethodInvocation" value="tru ...
- 【Python】【学习笔记】1.快速入门
1.软件安装 从官网下载相应版本的安装包,一般不大. https://www.python.org/ 安装一路默认即可 2. 参考教程:快速入门:十分钟学会Python 本文的内容介于教程(Totur ...
- JS-DOM基础
1 JS-DOM 全称:document object model 1.1 获取页面元素 getElementsByTagName():无论元素的数量是多少,都会存入数组 getElement ...