好,开始没做出来 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 ...
随机推荐
- .NET设计模式(10):装饰模式(Decorator Pattern)(转)
概述 在软件系统中,有时候我们会使用继承来扩展对象的功能,但是由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性:并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多 ...
- 如何做到尽可能不使用庞大的jQuery
jQuery 是现在最流行的 JavaScript 工具库. 据统计,目前全世界 57.3% 的网站使用它.也就是说,10 个网站里面,有 6 个使用 jQuery.如果只考察使用工具库的网站,这个比 ...
- Sqli-labs less 16
Less-16 本关我们的处理方法和less15是一样的,同样的使用延时注入的方法进行解决.这里直接从源代码中看到对id进行 ("id")的处理.(请自行测试) 提交的payloa ...
- 制作类似DataGrid自定义控件
首先看一下.net自带的DataGrid,想想如何应该怎样才能实现那样的展现形式. 1)需要以网格形式显示内容. 2)网格的宽度.高度可以定义. 3)可以显示滚动条. 4)单击可以选中某个单元格. 当 ...
- 使用时间戳引入css、js文件
前言 最近在一家创业公司实习,主要负责新版官网和商家平台管理系统的前端开发和维护,每次测试都要上传文件到ftp服务器端测试,初期由于更新修改比较频繁,每次都是直接上传覆盖css.js.php文件,链接 ...
- foreach的参数不是数组:Warning: Invalid argument supplied for foreach
Warning: Invalid argument supplied for foreach() 问题Warning: Invalid argument supplied for foreach() ...
- 2014多校第三场1005 || HDU 4891 The Great Pan(模拟)
题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中 ...
- 配置IIS应用程序池
IIS 6的核心在于工作进程隔离模式,而应用程序池则是定义工作进程如何进行工作,因此,可以说应用程序池是整个IIS 6的核心. 和IIS 5中只能使用单个应用程序池不同,工作在工作进程隔离模式的IIS ...
- 【Apache运维基础(5)】Apache的Rewrite攻略(2)
简述 .htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作 ...
- WPF之通过EventTrigger修改模板中元素的属性
前言:对于此操作,我只想说是微软的神经,还是我的笨蛋.为什么EventTrigger就不能像Trigger那样直接设置Property以及Value就对属性进行操作,而必须要放一个Action,而默认 ...