可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值#include #define MAX 1000#define max(a,b) ( (a)>(b)?(a):(b) )int dp[MAX]={0};int rob1(int* a, int n) { int i; ...
分类:
其他好文 时间:
2015-05-30 19:47:09
阅读次数:
112
动态规划,构造dp[n][2]数组,
dp[i][0]: 在不取nums[i]的情况下,从nums[0, 1, ..., n]中能获得的最大值;
dp[i][1]: 在取nums[i]的情况下,从nums[0], 1, ..., n]中能获得的最大值。
注意到,max(dp[i][0], dp[i][1])表征了从nums[0, 1, ..., n]中获得的最大值。
初始化:
dp[0][0] = 0;
dp[0][1] = nums[1].
状态方程:
dp[i][0] = max(dp[i-1][0...
分类:
其他好文 时间:
2015-05-30 16:43:39
阅读次数:
108
简单的动态规划状态方程:dp[i]=max{dp[i-1],dp[i-2]+a[i]}.//其实就是分为选择a[i]和不选择a[i]两种情况,取最大值。代码如下:#define MAX 1000#define max(a,b) ( (a)>(b)?(a):(b) )int dp[MAX]={0};i...
分类:
其他好文 时间:
2015-05-30 16:28:26
阅读次数:
92
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping yo...
分类:
其他好文 时间:
2015-05-30 01:48:23
阅读次数:
101
House Robber II问题:Note:This is an extension ofHouse Robber.After robbing those houses on that street, the thief has found himself a new place for his ...
分类:
其他好文 时间:
2015-05-28 00:42:45
阅读次数:
158
IYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...
分类:
其他好文 时间:
2015-05-27 06:17:38
阅读次数:
105
House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much...
分类:
其他好文 时间:
2015-05-23 00:07:43
阅读次数:
136
LeetCode House Robber II题目思路思路来源于Discuss。
一是在Robber这题中的O(n)解法;
二是在Robber这题中,我们只需要分别考虑包含了nums[0]和nums[n-1]的情况即可。
注意这里的包含并不是说Robber一定要偷num[0]或nums[n-1],只是说考虑进去的意思。代码#define max(a, b) ((a)>(b)?(a):(b))...
分类:
其他好文 时间:
2015-05-21 09:06:49
阅读次数:
155
leetcode 213 : House Robber II...
分类:
其他好文 时间:
2015-05-21 07:50:58
阅读次数:
115
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that...
分类:
其他好文 时间:
2015-05-21 06:35:47
阅读次数:
114