2.Dynamic Programming on Stolen Values【dp】
2013-10-31 21:48
393 查看
Problem: There are n houses built in a line, each of which contains some value in it. A thief is going to steal the maximal value in these houses, but he cannot steal in two adjacent houses because the owner of a stolen house will tell his two neighbors on the left and right side. What is the maximal stolen value?
For example, if there are four houses with values {6, 1, 2, 7}, the maximal stolen value is 13 when the first and fourth houses are stolen.
My Code:
Harry He:
Analysis: A function
f(
i) is defined to denote the maximal stolen value from the first house to the
ithhouse, and the value contained in the
ith house is denoted as
vi. When the thief reaches the
ithhouse, he has two choices: to steal or not. Therefore,
f(
i) can be defined with the following equation:
It would be much more efficient to calculate in bottom-up order than to calculate recursively. It looks like a 1D array with size
n is needed, but actually it is only necessary to cache two values for
f(
i-1) and
f(
i-2) to calculate
f(
i).
This algorithm can be implemented with the following C++ code:
int maxStolenValue(
const vector<
int>& values)
{
int length = values.size();
if(length == 0)
return 0;
int value1 = values[0];
if(length == 1)
return value1;
int value2 = max<
int>(values[0], values[1]);
if(length == 2)
return value2;
int value;
for(
int i = 2; i < length; ++i)
{
value = max<
int>(value2, value1 + values[i]);
value1 = value2;
value2 = value;
}
return value;
}
More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on
Amazon.com, or
Apress.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.
For example, if there are four houses with values {6, 1, 2, 7}, the maximal stolen value is 13 when the first and fourth houses are stolen.
My Code:
#include <iostream> #include <string.h> using namespace std; int dp[100]; int main() { memset(dp,0,sizeof(dp)); const int len=6; int a[len]={6,1,33,7,11,13}; for(int i=0;i<len;i++) { if(i<2) { if(i==0) dp[i]=a[i]; else if(i==1) dp[i]=a[i]>a[i-1]?a[i]:a[i-1]; } else { dp[i]=dp[i-2]+a[i]>dp[i-1]?dp[i-2]+a[i]:dp[i-1]; } } cout<<dp[len-1]<<endl; return 0; }
Harry He:
Analysis: A function
f(
i) is defined to denote the maximal stolen value from the first house to the
ithhouse, and the value contained in the
ith house is denoted as
vi. When the thief reaches the
ithhouse, he has two choices: to steal or not. Therefore,
f(
i) can be defined with the following equation:
It would be much more efficient to calculate in bottom-up order than to calculate recursively. It looks like a 1D array with size
n is needed, but actually it is only necessary to cache two values for
f(
i-1) and
f(
i-2) to calculate
f(
i).
This algorithm can be implemented with the following C++ code:
int maxStolenValue(
const vector<
int>& values)
{
int length = values.size();
if(length == 0)
return 0;
int value1 = values[0];
if(length == 1)
return value1;
int value2 = max<
int>(values[0], values[1]);
if(length == 2)
return value2;
int value;
for(
int i = 2; i < length; ++i)
{
value = max<
int>(value2, value1 + values[i]);
value1 = value2;
value2 = value;
}
return value;
}
More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on
Amazon.com, or
Apress.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.
相关文章推荐
- hdu 1421 搬寝室 dp问题
- hdu 3466 DP
- dp、sp和px的区别【Android】
- hdu 1024 dp
- 【树形dp】UVALive 2038 Strategic game
- UVA 6624(简单概率dp)
- bzoj1026: [SCOI2009]windy数-数位DP
- [AC自动机 DP] BZOJ 1030 [JSOI2007] 文本生成器
- BZOJ 2037 区间DP
- 2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数
- Fruit HDU - 2152 (dp计数问题)
- HiHo第173周 A Game 区间DP
- 【POJ1185】炮兵阵地(状压DP)
- hdu_1054 树结构dp 最小覆盖数
- hdu 4604 Deque(DP递增递减序列,4级)
- poj3280(回文串,DP)
- poj 2288 状态压缩dp
- hihoCoder 1076 与链 (数位dp)
- LeetCode_DP_Unique Binary Search Trees II
- BZOJ 2111: [ZJOI2010]Perm 排列计数|组合数学|Lucas定理|DP