思路:

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. Ubuntu 16.04安装Atom(加强版文本工具)

    安装: sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo apt-get install atom 或者直接上 ...

  2. 再探gdb经常使用命令

     前面已经有了一篇对gdb经常使用命令的总结.见 http://blog.csdn.net/u011848617/article/details/12838875 这里对眼下学过的gdb命令进行了 ...

  3. luajit利用ffi结合C语言实现面向对象的封装库

    luajit中.利用ffi能够嵌入C.眼下luajit的最新版是2.0.4,在这之前的版本号我还不清楚这个扩展库详细怎么样,只是在2.04中,真的非常爽.  既然是嵌入C代码.那么要说让lua支持 ...

  4. storm与hadoop的对照

       hadoop 是实现了 mapreduce 的思想,将数据切片计算来处理大量的离线数据. hadoop处理的数据必须是已经存放在 hdfs 上或者类似 hbase 的数据库中.所以 hadoop ...

  5. TQ210--UBOOT移植笔记--添加自己的单板【学习笔记】

    在uboot的源码的根目录下的readme中有介绍如何在uboot中添加自己的单板: 一.在boards.cfg中添加自己的单板的信息,可以模仿smdkc100去添加自己的单板的信息 二.复制单板的配 ...

  6. NSubstitute

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

  7. POJ 3650:The Seven Percent Solution

    The Seven Percent Solution Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7684   Accep ...

  8. JFreeChart简单用法

    需要用到的包 jfreechart-0.9.20.jar,jcommon-0.9.5.jar 创建一般步骤: 1.生成org.jfree.data.DefaultCategoryDataset对象,方 ...

  9. poj 3683(2-sat+拓扑排序)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11127   Accep ...

  10. uva10870

    https://vjudge.net/problem/UVA-10870 裸的矩阵快速幂 注意系数矩阵在前面 因为系数矩阵为d*d 方程矩阵为d * 1 放反了就是d * 1 d * d 不符合矩阵乘 ...