leetcode375 Guess Number Higher or Lower II
思路:
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的更多相关文章
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- 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 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 前端自动化测试工具--使用karma进行javascript单元测试(转)
Karma+Jasmine+PhantomJS组合的前端javascript单元测试工具. 1.介绍 Karma是由Google团队开发的一套前端测试运行框架,karma会启动一个web服务器,将js ...
- Chrom开发者工具详解
Chrome开发者工具不完全指南(一.基础功能篇) http://www.mamicode.com/info-detail-863534.html Chrome开发者工具不完全指南(二.进阶篇) ht ...
- JButton点击事件
JButton点击事件: 以前都是搞一个JFrame,放个JButton,然后用鼠标点击: 忽然之间: import javax.swing.JButton; public class Page06 ...
- centos7 禁止 root ssh login
CentOS 7 默认容许任何帐号透过 ssh 登入,包括 root 和一般帐号,为了不让 root 帐号被黑客暴力入侵,我们必须禁止 root 帐号的 ssh 功能,事实上 root 也没有必要 s ...
- Java RMI之HelloWorld程序以及相关的安全管理器的知识
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,可以让在某个 Java 虚拟机上的对象调用还有一个 Java 虚拟机中的对象上的方法.可以用此 ...
- HDU 5319 Painter(枚举)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...
- Razor基础,视图里如何调用controller里的函数
1.单行代码书写 @代码 2.多行代码书写 @{ //@加个大括号就可以在里面写C#代码了. C#代码第一行 C#代码第二行 } 3.Razor模板引擎会自动判别代码块,但是当代码存在二义性的时候,可 ...
- text recognizer (OCR) Engine 光学字符识别
https://github.com/tesseract-ocr/tesseract/wiki https://github.com/UB-Mannheim/tesseract/wiki C:\Use ...
- HDU 1023 Catalan数+高精度
链接:HDU 1023 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:5 ...