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. windows和linux下的spice客户端使用方法

    1.Linux客户端 安装spice yum install virt-viewer 连接远程虚拟机 #remote-viewer spice://IP:PORTremote-viewer spice ...

  2. Deep Module(深模块)

    Deep Module(深模块) 目录 1,模块化设计 2,接口里有什么 3,抽象 4,深模块 5,浅模块 6,Classitis 7,例子 8,结论 正文 类是不是越小越好?最近在读John Ous ...

  3. k8s的包管理

    1.Helm的概念和架构 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Helm 则是 Kubernetes 上的 ...

  4. Spring Boot源码探索——自动配置的内部实现

    前面写了两篇文章 <Spring Boot自动配置的魔法是怎么实现的>和 <Spring Boot起步依赖:定制starter>,分别分析了Spring Boot的自动配置和起 ...

  5. ReLU激活函数

    参考:https://blog.csdn.net/cherrylvlei/article/details/53149381 首先,我们来看一下ReLU激活函数的形式,如下图: 单侧抑制,当模型增加N层 ...

  6. Mybatis的一级缓存机制简介

    1.接口 public interface MemberMapperCache { public Members selectMembersById(Integer id); } 2.配置文件xml ...

  7. 理解*arg 、**kwargs

    这两个是python中的可变参数.*args表示任何多个无名参数,它是一个tuple(元祖):**kwargs表示关键字参数,它是一个dict(字典).并且同时使用*args和**kwargs时,必须 ...

  8. Linux 介绍与安装

  9. 54、servlet3.0-ServletContainerInitializer

    54.servlet3.0-ServletContainerInitializer Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.S ...

  10. 43、扩展原理-@EventListener与SmartInitializingSingleton

    43.扩展原理-@EventListener与SmartInitializingSingleton 还可以使用 @EventListener; 来监听事件 原理:使用EventListenerMeth ...