好,开始没做出来 guess-number-higher-or-lower-ii
https://leetcode.com/mockinterview/session/result/xsicjnm/
https://leetcode.com/problems/guess-number-higher-or-lower-ii/
// https://discuss.leetcode.com/topic/51353/simple-dp-solution-with-explanation
// https://en.wikipedia.org/wiki/Minimax
// 开始我的思路有问题,我是先选择区间,最后收敛到结果数
// 实际上work的思路是,先选择数字,再走向某个区间,然后取两个区间中的更大值 class Solution {
int ** table;
int DP(int s, int e) {
if (s >= e) {
return 0;
} if (table[s][e] != INT_MAX) {
return table[s][e];
}
int local_max = INT_MAX;
for (int k=s; k<=e; ++k) {
// 下面这个表达式很重要
local_max = min(k + max(DP(s, k-1), DP(k+1, e)), local_max);
}
table[s][e] = local_max;
return local_max;
} public:
int getMoneyAmount(int n) { table = new int*[n+1];
for (int i=0; i<n+1; ++i) {
table[i] = new int[n+1];
for (int j=0; j<n+1; ++j) {
table[i][j] = INT_MAX;
}
} int ret = DP(1, n); for (int i=0; i<n+1; ++i) {
delete[] table[i];
}
delete[] table; return ret;
} }; // 用Java又做了一遍 package com.company; import java.util.ArrayList;
import java.util.List; class Solution {
int[][] dp; int get(int s, int e) {
if (s >= e) {
// 注意,只有一个的话,不用猜
return 0;
} if (dp[s][e] != 0) {
return dp[s][e];
} // 注意Java的是这种形式的MIN/MAX
int min = Integer.MAX_VALUE;
for (int i=s; i<=e; i++) {
int tmp = Math.max(get(s, i-1), get(i+1, e)) + i;
if (tmp < min) {
min = tmp;
}
}
dp[s][e] = min;
return min;
} public int getMoneyAmount(int n) {
// 看了之前做的内容,思路还是很清晰的
// 要用DP的时候,不要犹豫 dp =new int[n+1][n+1];
int ret = get(1, n);
return ret;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello");
Solution solution = new Solution(); int ret = solution.getMoneyAmount(3);
System.out.printf("Get ret: %d\n", ret); }
}
好,开始没做出来 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 ...
- 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: 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 ...
- [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 ...
随机推荐
- HDU1048The Hardest Problem Ever
The Hardest Problem Ever Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- wordpress无法安装这个包。: PCLZIP_ERR_MISSING_FILE (-4) : Missing archive file 'C:\WINDOWS\TEMP/wordpress-4.tmp'
朋友的wp博客好久没管理了,让ytkah帮忙打理一下,进到后台发现版本还是3.9的,那是比较早以前的版本了,早该升级了. 在升级wordpress时出现以下错误: 无法安装这个包: PCLZIP_ER ...
- Sqli-labs less 58
Less-58 执行sql语句后,并没有返回数据库当中的数据,所以我们这里不能使用union联合注入,这里使用报错注入. Payload:http://127.0.0.1/sqli-labs/Less ...
- awk处理之案例三:awk去掉不需要的文本行
编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...
- iOS音效
//AudioToolbox.framework是一套基于C语言的框架,使用它来播放音效其本质是将短音频注册到系统声音服务(System Sound Service) //System Sound S ...
- ResourceBundle使用
一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的语言 一次处理多个语言环境 ...
- 最大 / 小的K个数
在<剑指offer>上看到的,而且Qunar去年的校招笔试也考了这题,今天晚上去西电腾讯的宣讲会,来宣讲的学长也说他当时一面的时候面试官问了“一亿个数据的最大的十个数”的面试题.今晚就写写 ...
- linux下如何查看主机的外网ip地址
在linux下如果我们使用的是nat方式上网.通过ifconfig命令查看到的ip地址往往是内网地址 那么如何查看主机在互联网上使用的公网IP呢?我们可以在命令行下使用curl命令实现这个功能. [r ...
- 浏览器的CSS各种hack,大汇总
对着IE久了也有感觉了,在win10出新浏览器以及中国的IE6+用户没有普及新的浏览器前IE还是个坑,所以hack这东西还是要掌握一点的.不废话直接贴图 记得之前在项目里面针对IE6的hack是这样写 ...
- Gradle Goodness: Rename Ant Task Names When Importing Ant Build File
Migrating from Ant to Gradle is very easy with the importBuild method from AntBuilder. We only have ...