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 = , I pick .

 First round:  You guess , I tell you that it's higher. You pay $5.
Second round: You guess , I tell you that it's higher. You pay $7.
Third round: You guess , I tell you that it's lower. You pay $9. Game over. is the number I picked. You end up paying $ + $ + $ = $.

Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.

思路:DP。

设OPT(i, j)表示i到j范围内的最优解。假设我们猜的数字是k,则i <= k <= j。之后,k将该问题分解为两个子问题,OPT(i, k-1)和OPT(k+1, j)。我们要的是其中的最坏情况。因此OPT(i, j) = min(k + max(OPT(i, k-1), OPT(k+1, j)))

复杂度为O(n^3).

 class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int> > dp(n + , vector<int>(n + , ));
for (int st = n; st > ; st--) {
for (int ed = st + ; ed <= n; ed++) {
int localMin = INT_MAX;
for (int k = st; k <= ed; k++) {
int cur = k + std::max(k == st ? : dp[st][k-], k == ed ? : dp[k+][ed]);
localMin = std::min(cur, localMin);
}
dp[st][ed] = localMin;
}
}
return dp[][n];
}
};

Guess Number Higher or Lower II -- LeetCode的更多相关文章

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

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

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

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

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

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

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

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

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

  8. Leetcode: Guess Number Higher or Lower II

    e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...

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

随机推荐

  1. 每天一个Linux命令(3):ls命令

    ls命令用来显示目标列表,在Linux中是使用率较高的命令.ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件. 语法 ls(选项)(参数) 选项 -a:显示所有档案及目录(ls内定将档案名 ...

  2. python3 虚拟环境配置

    CentOS7 python3 虚拟环境配置 1. 安装依赖包 yum -y install wget gcc epel-release git 2. 安装 Python3.6 yum -y inst ...

  3. 构建Docker镜像两种方式的比较-Dockerfile方式和S2I方式

    前言 写Dockerfile是构建Docker镜像最通常的方式,接触过Docker的童鞋多少了解一些.前段时间研究OpenShift(paas的一种),发现了另外一种构建Docker镜像的方式:S2I ...

  4. Small组件化重构安卓项目

    如果从一开始就没有设计好 后面项目业务比较大的时候很难掉头

  5. 洛谷树剖模板题 P3384 | 树链剖分

    原题链接 对于以u为根的子树,后代节点的dfn显然比他的dfn大,我们可以记录一下回溯到u的dfn,显然这两个dfn构成了一个连续区间,代表u及u的子树 剩下的就和树剖一样了 #include< ...

  6. java爬虫--使用正则表达式获取网页中的email

    package com.enation.newtest; import java.io.*; import java.util.regex.*; import java.net.*; public c ...

  7. Pandas之Series

    # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引 import numpy as np impor ...

  8. package-lock.json 文件的作用

    npm5之后安装文件之后会多出一个package-lock.json的文件,它的作用是: 1. 安装之后锁定包的版本,手动更改package.json文件安装将不会更新包,想要更新只能使用 npm i ...

  9. AGC023E - Inversion

    Description \(n \le 2*10^5\) 给定限制序列 \(A\) 求满足 \(P_i\le A_i\) 的所有排列中 逆序对个数的和 Solution 考虑知道一个 \(A\) 序列 ...

  10. 51nod1086 背包问题 V2——二进制优化

    有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...