思路:

dp。

https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/

实现:

 class Solution
{
public:
int getMoneyAmount(int n)
{
int dp[n + ][n + ];
memset(dp, 0x3f, sizeof dp);
for (int i = ; i <= n; i++)
dp[i][i] = ;
for (int i = n - ; i >= ; i--)
{
for (int j = i + ; j <= n; j++)
{
for (int k = i; k <= j; k++)
{
dp[i][j] = min(dp[i][j], k +
max(i > k - ? : dp[i][k - ],
k + > j ? : dp[k + ][j]));
}
}
}
return dp[][n];
}
};

 实现2:

 class Solution
{
public:
int dfs(int l, int r, vector<vector<int>> & dp)
{
if (l == r) return ;
if (dp[l][r] != -) return dp[l][r];
int ans = 0x3f3f3f3f;
ans = min(ans, l + dfs(l + , r, dp));
ans = min(ans, r + dfs(l, r - , dp));
for (int i = l + ; i <= r - ; i++)
{
ans = min(ans, max(dfs(l, i - , dp), dfs(i + , r, dp)) + i);
}
return dp[l][r] = ans;
}
int getMoneyAmount(int n)
{
vector<vector<int>> dp(n + , vector<int>(n + , -));
return dfs(, n, dp);
}
};

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

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

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

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

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

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

  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. [Swift]LeetCode375. 猜数字大小 II | 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 ...

  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. mysql计算两个日期之间的天数

    MYSQL自带函数计算给定的两个日期的间隔天数   有两个途径可获得   1.利用TO_DAYS函数   select to_days(now()) - to_days('20120512')   2 ...

  2. ZOJ 3819 Average Score 水

    水 Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Univer ...

  3. jmeter实现Http接口测试介绍

    构建一个测试页面 页面功能测试说到底就是模拟用户浏览点击页面的全过程,很多的测试工具都可以对该过程进行录制后模拟用户操作,而压力测试就是将这个过程在单位时间内重复成千上万次,看检测应用的高可用,接下来 ...

  4. 关于ListView的setEmptyView没效果的问题

    使用listView或者gridView时,当列表为空时.有时须要显示一个特殊的empty view来提示用户,普通情况下,假设你是继承ListActivity.仅仅要 TextView tv= ne ...

  5. ngnix 详解

    4 Nginx的rpm软件包安装 4.1 安装包在位置 D:\讲课内容--\新巴巴运动网\nginx高并发解决\nginx安装包 4.2 此种安装方式不用安装gcc等编译工具 4.3 安装命令如下 r ...

  6. [RK3288][Android6.0] 调试笔记 --- 录音音量从HAL到APP层会变小问题【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/72783843?locationNum=9&fps=1 Platform: Rockc ...

  7. NSubstitute

    https://github.com/nsubstitute/NSubstitute http://nsubstitute.github.io/help/creating-a-substitute/

  8. Ubuntu 16.04下安装MacBuntu 16.04 TP 变身Mac OS X主题风格

    Ubuntu 16.04下安装MacBuntu 16.04 TP 变身Mac OS X主题风格 sudo add-apt-repository ppa:noobslab/macbuntu sudo a ...

  9. URAL 1003,1004

    1003: 并查集在处理矛盾关系的应用,讲的比较好的题解 #include <map> #include <set> #include <list> #includ ...

  10. Flask的jinja2模板中自定义过滤器的使用

    大部分的模板引擎都是支持过滤器功能的,jinja2也不例外,它提供了丰富的内置过滤器,但是有些时候还是没办法满足我们的需求,好在jinja2支持自定义过滤器,下面是一个简单的例子. 下面的例子完成了自 ...