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 ...
随机推荐
- oracle索引简单使用
--查询表索引 select * from user_ind_columns where table_name = upper('HY_PROJECT') and column_name = uppe ...
- Oracle生成多表触发器sql
--将所有HY开头的表都生成一个更新触发器的脚本('/'是为了连续创建多个触发器而不报错)select 'CREATE OR REPLACE TRIGGER '||table_name||' BEFO ...
- cache and database
This article referenced from http://coolshell.cn/articles/17416.html We all know that high concurren ...
- CSS3的animation功能
旋转动画 <img src="https://facebook.github.io/react/img/logo.svg" class="App-logo" ...
- symfony could not load type 'datetime'
当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...
- [React] Use Prop Collections with Render Props
Sometimes you have common use cases that require common props to be applied to certain elements. You ...
- 二叉查找树(BST)
二叉查找树(BST) 二叉查找树(Binary Search Tree)又叫二叉排序树(Binary Sort Tree),它是一种数据结构,支持多种动态集合操作,如 Search.Insert.De ...
- Spring MVC 数据验证——validate编码方式
1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...
- Windows 平台上长路径名文件的解决方法
https://www.ibm.com/developerworks/cn/java/j-lo-longpath.html
- Delphi中SendMessage使用说明(所有消息说明) good
Delphi中SendMessage使用说明 SendMessage基础知识 函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数Po ...