做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 (-11, 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:

  1. 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.
  2. Take a small example (n = 3). What do you end up paying in the worst case?
  3. Check out this article if you're still stuck.
  4. The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming.
  5. 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的更多相关文章

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

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

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

  3. [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 ...

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

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

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

  6. 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 ...

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

  8. 375. Guess Number Higher or Lower II

    最后更新 四刷? 极大极小算法..还是叫极小极大的.. 首先要看怎么能保证赢. 比如2个数,猜第一个猜第二个都能保证下一轮我们赢定了,为了少交钱,我们猜小的. 比如3个数,猜第二个才能保证下一轮再猜一 ...

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

    我们正在玩一个猜数游戏,游戏规则如下:我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字.每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了.然而,当你猜了数字 x 并且猜错了的时候,你需 ...

随机推荐

  1. 在webstorm设置File watcher for Jade

    用Jade模板引擎写html确实方便,元素不用闭合,很多种简写的方法. 为了要知道自己写的对不对,就要用到jade -w命令监控jade文件,只要变化就编译. 现在用webstorm写代码的超多,可以 ...

  2. ASP.NET Core开发-Docker部署运行

    ASP.NET Core开发Docker部署,.NET Core支持Docker 部署运行.我们将ASP.NET Core 部署在Docker 上运行. 大家可能都见识过Docker ,今天我们就详细 ...

  3. Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]

    [虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...

  4. Mybatis中javaType和jdbcType对应关系

    JDBC Type           Java Type CHAR                  String VARCHAR            String LONGVARCHAR    ...

  5. ASP.NET Redis 开发

    文件并发(日志处理)--队列--Redis+Log4Net Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高 ...

  6. springmvc的初始化参数绑定

    一.springmvc的初始化参数绑定 此种和我们之前说的类型转换非常相似,可以看作是一种类型转换 在初始化参数绑定时  重要的是参数类型 -------------------单日期的绑定 二. 配 ...

  7. 移动H5前端性能优化指南

    移动H5前端性能优化指南 概述 1. PC优化手段在Mobile侧同样适用2. 在Mobile侧我们提出三秒种渲染完成首屏指标3. 基于第二点,首屏加载3秒完成或使用Loading4. 基于联通3G网 ...

  8. ipa如何通过网络进行安装

    苹果手机端应用,如果发布的到Appstore上,往往比较复杂,周期也比较长,Over-the-Air是Apple在 iOS4 中新加的一项技术,目的是让开发者能够脱离Appstore,实现从自己的服务 ...

  9. javascript实现下雪效果

    原理: 1.js动态创建DIV,指定CLASS类设置不同的背景图样式显示不同的雪花效果. 2.js获取创建的DIV并改变其top属性值,当下落的高度大于屏幕高后删除该移动div 3.好像不够完善勿喷 ...

  10. .html(),.text()和.val()的差异总结

    .html(),.text(),.val()三种方法都是用来读取选定元素的内容:只不过.html()是用来读取元素的html内容(包括html标签),.text()用来读取元素的纯文本内容,包括其后代 ...