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:
#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.

2.Dynamic Programming on Stolen Values【dp】的更多相关文章

  1. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. SQLSERVER2014集群实战——IP引发的坑

    在之前的帖子里有提到过,为了避免IP变更带来的一系列问题,采取了不改变IP的策略.原以为,只要SQLSERVER的群集IP保持与之前单机部署的IP一致,就基本上不会有问题.然而实际永远比预想的更复杂. ...

  2. hdu 1518 dfs+剪枝

    题目大意:几根棒子能否组成一个正方形 Sample Input3           //测试组数4 1 1 1 1   //棒子数目以及每根棒子的长度5 10 20 30 40 508 1 7 2 ...

  3. zookeeper【3】服务发现

    服务发现:指对集群中的服务上下线做统一管理,每个工作服务器都可以作为数据的发布方,向集群注册自己的基本信息,而让某些监控服务器作为订阅方,订阅工作服务器的基本信息.当工作服务器的基本信息改变时,如服务 ...

  4. Android 打包出现jdk版本错误的问题

    Android 打包出现 jdk 版本错误的问题,本质上是 SDK 的问题,与 JDK 无关.如果 SDK 的 API 是24或者更高,就要求 jdk 1.8,我这里指定的 API 是22,所以去勾选 ...

  5. EXPLAIN 用法

    重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...

  6. 活动(Activity)

    一.用Log打印日志 Log.d("HelloWorldActivity", "onCreate execute"); 二.Toast用法 Toast.make ...

  7. SSM框架搭建问题

    环境: 1.eclipse  Kepler Service Release 2 2.jdk 1.8 64 3.maven 3.5 4.tomcat 8 问题:

  8. 我对NHibernate的感受(4):令人欣喜的Interceptor机制

    之前谈了NHibernate的几个方面,似乎抱怨的居多,不过这次我想谈一下我对Interceptor的感受,则基本上都是好话了.这并不一定是说Interceptor设计的又多么好(事实上它使用起来还是 ...

  9. 在Windows Server 2008 R2中使用web方式修改域用户账户密码

    在Windows的domain环境下,加域的客户端修改账户密码是一件很easy的事情:即使没有加域的客户端如果组织中,使用Exchange邮件系统,借助Exchange的owa也可以轻松修改账户密码. ...

  10. C#引用类型转换,到底使用is,as还是显式强转?

    在C#中,当引用类型需要转换的时候,经常会用到关键字is.as以及显式强转.本篇来体验这三者的用法. 先来梳理.NET引用类型转换的"约定俗成",或者叫"惯例" ...