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.

Runtime: 56 ms, faster than 38.71% of C++ online submissions for Guess Number Higher or Lower II.
Memory Usage: 5.4 MB, less than 0.56% of C++ online submissions for Guess Number Higher or Lower II.
 
class Solution {
vector<vector<int>>dp;
public:
int getMoneyAmount(int n) {
dp.resize(n+, vector<int>(n+,));
return DP(,n);
} int DP(int s, int e) {
if(s >= e) return ;
if(dp[s][e] != ) return dp[s][e];
int res = INT32_MAX;
for(int x=s; x<=e; x++) {
int tmp = x + max(DP(s,x-),DP(x+,e));
res = min(res, tmp);
}
dp[s][e] = res;
return res;
}
};

LC 375. Guess Number Higher or Lower II的更多相关文章

  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 375. Guess Number Higher or Lower II

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

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

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

  7. 375. Guess Number Higher or Lower II

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

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

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

  9. [leetcode]375 Guess Number Higher or Lower II (Medium)

    原题 思路: miniMax+DP dp[i][j]保存在i到j范围内,猜中这个数字需要花费的最少 money. "至少需要的花费",就要我们 "做最坏的打算,尽最大的努 ...

随机推荐

  1. c# 属性说明

  2. window.addEventListener('error')监听页面是否更新版本

    因本司更新迭代的速度很快,有时候更改一个BUG就要马上更新版本,就会引起用户在应用当中,页面点击无反应,其实是打包的js和css的包名称更改,找不到以前的包的缘故.我现在用一个小方法,判断js或css ...

  3. 集合(python)

    # -*- coding: utf-8 -*- class Array(object): def __init__(self, size=32, init=None): self._size = si ...

  4. WPF 反编译后错误处理

    1. 首先,手动创建一个WPF工程(WpfApplicationReflectorDemo) 2. 把生成的WpfApplicationReflectorDemo.exe 拖到ILSpy里 3.点击 ...

  5. Multiple inheritance in Go

    原文:http://golangtutorials.blogspot.com/2011/06/multiple-inheritance-in-go.html --------------------- ...

  6. 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream

    一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...

  7. 前端面试题-JavaScript

    引用GitHub 上 ltadpoles的前端面试 https://github.com/ltadpoles 目录  1. JavaScript 有哪些数据类型  2. 怎么判断不同的JS数据类型   ...

  8. 基于jQuery制作的手风琴折叠菜单

    初始化为全部隐藏 点第一个,显示第一个所隐藏的内容 当点第二个的时候,第一个的内容隐藏,第二个栏目的内容显示,以此类推 下面是代码部分 <!DOCTYPE html><html la ...

  9. 063_显示本机 Linux 系统上所有开放的端口列表

    #!/bin/bash#从端口列表中观测有没有没用的端口,有的话可以将该端口对应的服务关闭,防止意外的攻击可能性 ss -nutlp |awk '{print $1,$5}' | awk -F&quo ...

  10. 2019ICPC徐州自我反省及未来打算

    徐州站结束了有好几天了,然而为了热爱的网络课(qdu-zpj网络课你值得信赖),一直没时间写个博客,今天又来说点心里话 今年的ICPC,就这样都打完了,可惜最终也是没能拿金,不过拿到了块银,也算保底吧 ...