374&375. Guess Number Higher or Lower 1&2
做leetcode的题
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 is higher or lower.
You call a pre-defined API guess(int num)
which returns 3 possible results (-1
, 1
, or 0
):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example:
n = 10, I pick 6. Return 6. 基本就是这样:关键在于怎么找,怎么去guess; 基本点:查找/随机;(参数)递归;
进阶点:TLE错误解决——
mid = (low + high) / 2;
mid = low + (high - low) / 2;
第一种计算方法会Time Limit Exceeded,原因可能是(low + high)的结果超过int的最大范围,越界。
可以使用第一种公式,但把数据改成long型;也可以改成mid = low/2 + high/2公式。
引用自http://blog.csdn.net/y12345678904/article/details/51898958;
然后注意审题,看清楚guess和guessNum
/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */ public class Solution extends GuessGame {
public int guessNumber(int n) {
if (guess(n)== 0) return n;
int left=0;
int right=n;
while (left<right) {
int mid=left+(right-left)/2;
int re=guess(mid);
if (re==0){
return mid;
}else if(guess(mid)==-1){
right=mid;
}else{
left=mid;
}
// return left;
}
return left;
}
}
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 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.
Hint:
- The best strategy to play the game is to minimize the maximum loss you could possibly face. Another strategy is to minimize the expected loss. Here, we are interested in thefirst scenario.
- Take a small example (n = 3). What do you end up paying in the worst case?
- Check out this article if you're still stuck.
- The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming.
- As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss?
基本思路:最大值最小化算法(更新:用最小化的方法(这里是二分法),找到所有情况,取最大值,就是题目中需要的“至少”);也就是求的是最大值,但是是最大值中的最小的那一个;那么逻辑应该是很清晰的,两步,找到最大值,再找最大值的最小值;
基本实现:递归;——这里特别吊的是别个用了个二维数组来比较,用二维数组的序号/位置,来分析n个数的情况——http://www.cnblogs.com/neweracoding/p/5679936.html;
public class Solution {
public int getMoneyAmount(int n) {
int[][] table = new int[n + 1][n + 1]; //0
return payForRange(table, 1, n);
} //return the amount paid for the game within range [start,end]
private int payForRange(int[][] dp, int start, int end) {
if (start >= end)
return 0;
if (dp[start][end] != 0)
return dp[start][end]; int minimumForCurrentRange = Integer.MAX_VALUE;
for (int x = start; x <= end; ++x) {
//calculate the amount to pay if pick x.
int pay = x + Math.max(payForRange(dp, start, x - 1), payForRange(dp, x + 1, end));
//calculate min of maxes
minimumForCurrentRange = Math.min(minimumForCurrentRange, pay);
} dp[start][end] = minimumForCurrentRange;
return minimumForCurrentRange;
}
}
这个递归用的我心服口服。。。。
374&375. Guess Number Higher or Lower 1&2的更多相关文章
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 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] 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 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
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 375. Guess Number Higher or Lower II
最后更新 四刷? 极大极小算法..还是叫极小极大的.. 首先要看怎么能保证赢. 比如2个数,猜第一个猜第二个都能保证下一轮我们赢定了,为了少交钱,我们猜小的. 比如3个数,猜第二个才能保证下一轮再猜一 ...
- 375 Guess Number Higher or Lower II 猜数字大小 II
我们正在玩一个猜数游戏,游戏规则如下:我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字.每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了.然而,当你猜了数字 x 并且猜错了的时候,你需 ...
随机推荐
- 火狐浏览器与Chorme的兼容性小问题
1.如果 result 返回时空字符串, 则在 火狐里面 回调函数不会进入执行. 如果是 谷歌浏览器,则会 执行回调. $.post(, ipagesize: , strSearchKey: strk ...
- 【T-SQL】分布抽取部分数据
好吧,我确实不知道该怎么起这个标题,整了一个“分布”,感觉还有点高档,其实没啥技术含量,看完你就知道了.情况是这样,刚刚接到一个临时任务,需要让几个营业点的销售数据[变]少一点,就是在ERP的相关报表 ...
- Android使用ViewPager实现左右循环滑动及轮播效果
边界的时候会看到一个不能翻页的动画,可能影响用户体验.此外,某些区域性的ViewPager(例如展示广告或者公告之类的ViewPager),可能需要自动轮播的效果,即用户在不用滑动的情况下就能够看到其 ...
- datagridview 单元格格式转换注意
datagridview 单元格内容进行比较时要注意正确写法要用强制转换,否则出错Convert.ToString(grd_order.SelectedRows[0].Cells[1].Value)= ...
- IBC编程社区
IBC编程社区-.NET编程交流论坛 官方地址:http://www.ibcibc.com 新浪微博:IBC编程社区 微信公众号:ibcbcsq QQ一群:235371874(已满) QQ二群:248 ...
- ComponentOne 2016 V3 发布
ComponentOne Studio Enterprise 2016 V3 新特性 我们很高兴的宣布ComponentOne 2016 V3发布了!2016 Connect开发者大会上微软发布了Vi ...
- oracle触发器详解
触发器是许多关系数据库系统都提供的一项技术.在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异常处理过程的PL/SQL块. 1.触发器类型 触发器在数据库里以独立的对象存储,它与存储过程和 ...
- ListView的CheckBox实现全部选中/不选中
在Adapter类中定义一个HashMap列表,保存每一行是否被选中: private static HashMap<Integer, Boolean> isSelected; 可见定义了 ...
- hibernate中表关系为多对多时,如何只删除中间表数据
先说问题:我遇到的问题是,在用户和用户组对象关系中他们是多对多关系.所以中间是成在一张中间表的.经理要求当逻辑删除对象数据时,必须删除中间表中的数据. hibernate是面向对象操作sql语句的,如 ...
- rest api参数与content-type
最近为项目组提供rest api 时遇到了关于接口参数的传递问题,主要是没有充分考虑到第三方调用者的使用方式,应该尽量的去兼容公司之前提供出去的接口调用方式,这样可以降低第三方调用者的学习成本,尽管之 ...