做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. ActiveX(二)Js 监听 ActiveX中的事件

    在上一篇随笔:ActiveX(一)第一个简单的Demo 中,已经可以实现 js 单向调用 ActiveX 中的方法,在很多情况下ActiveX中的方法的执行时相对耗时.耗性能的.在这样的情况下.Act ...

  2. 初入网络系列笔记(3)HTTP协议最简单的掌握

    一.借鉴说明,本博文借鉴以下博文 1.starok,HTTP必知必会,http://www.cnblogs.com/starstone/p/4890409.html 2.CareySon,HTTP协议 ...

  3. asp.net <asp:Content>控件

    <asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...

  4. sqlserver 通用分页存储过程(转)

    USE [AAA_TYDC] GO /****** Object: StoredProcedure [dbo].[proc_DataPagination] Script Date: 11/20/201 ...

  5. OA项目——总结

    先来张大致结构图: 项目链接:https://github.com/shuai7boy/YM_OA

  6. 【nodejs笔记2】认识express框架

    app.js:启动文件,或者说入口文件package.json:存储着工程的信息及模块依赖,当在 dependencies 中添加依赖的模块时,运行 npm install,npm 会检查当前目录下的 ...

  7. 2个很有趣、耐思考的C语言算法

    1. 输入10个整数,任意相邻的两个数不同,输出所有的递增,递减序列 比如: 输入:1 5 9 8 12 21 3 0 -1 9 输出: 1 5 9 9 8 8 12 21 21 3 0 -1 -1 ...

  8. BZOJ 1044 木棍分割 解题报告(二分+DP)

    来到机房刷了一道水(bian’tai)题.题目思想非常简单易懂(我的做法实际上参考了Evensgn 范学长,在此多谢范学长了) 题目摆上: 1044: [HAOI2008]木棍分割 Time Limi ...

  9. 我与ADO.NET二三事(2)

    继上篇开始,这里主要再次精简.因为大家已经熟悉了主要思想以及它的工作方式.那么这里提供的案例改动会很大.上篇的DatabaseCommand***均是为了大家能够轻松的理解所临时编写的.这次提供的接口 ...

  10. OData V4 系列 .net应用

    OData 学习目录 添加 OData Client Code Generator 扩展 添加OData T4生成工具 修改 T4 模板的 MetadataDocumentUri 运行Web项目,之后 ...