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 ...
随机推荐
- acm系统开发笔记
时间: 2016/2/29 遇到的困难: 数据库配置的mysql和java(Date)不一致,出现下面错误 Date date = new Date(); SimpleDateFormat ...
- hg0088新2网址
封装通俗的说,就是我有一些秘密不想让人知道,就通过私有化变量和私有化方法,这样外界就访问不到了.hg0088新2网址然后如果你有一些很想让大家知道的东西,你就可以通过this创建的属性看作是对象共有属 ...
- Extjs 源码组成(4.0.7)
(function(){})()形式的自执行,构建Ext对象(0~584) 1 设置全局对象EXt:global.Ext = {}, 2 实现了Ext对象面向对象编程的基础方法,如,apply,ex ...
- js的Promise学习笔记(1)
1: 何为Promise Promise是抽象异步处理对象以及对其对象进行各种操作的组件,是基于并列/并行处理设计的一种编程语言. 说到基于JavaScript的异步处理,大多数都会想到利用回调函数. ...
- 本地hosts临时域名访问
当刚购买了空间,域名和空间还未进行绑定,可以用临时域名访问主机调试网站.您可通过本地hosts指向访问网站,具体方法如下: 特别说明:设置以后,只有当前设置的电脑才能访问,其他电脑访问无效. 第一步: ...
- ubuntu下安装python各类运维用模块(以后补充用途)
环境:ubuntu 16.04LTS,python3,python2 已安装:pip3,pip2 注:基于Python自动化运维这本书上介绍的各模块而来 1.python-rrdtool(just f ...
- Linux系统程序的运行级别
Linux系统有7个运行级别: 运行级别 描述 0 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 1 但用户工作状态,root权限,用于系统维护,禁止远程登录 2 多用户状态(没有NFS ...
- 基于KEIL4开发ARM9(S3C2440)的裸机程序
本文主要介绍如何使用Keil4开发ARM9(S3C2440)裸机程序. 说明: 一.平台: 操作系统:Windows XP系统 KEIL版本:4.73 开发板:ARM9(S3C2440) 二.建立工程 ...
- Java 找不到主类错误
Eclipse 运行java 程序,突然出现错误:没有或找不到主类. 在网上找了好多办法,都不行. jdk环境配置啊-->这个一般不会出错,因为以前都不会出现这种问题. 查看项目配置啥的--&g ...
- c# unity PlayerPrefs 游戏存档,直白点就是讲游戏数据本地保存下来
在游戏会话中储存和访问游戏存档.这个是持久化数据储存,比如保存游戏记录. 我的理解是通过某个特殊的标签来保存在本地,而且该标签为key的意思,初始值不用赋值. 在游戏开发中较为实用. 暂时用到了 Se ...