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. 防止sql注入的几种方法

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  2. Java 的类加载顺序

    Java 的类加载顺序 一.加载顺序:先父类后子类,先静态后普通 1.父类的静态成员变量初始化 2.父类的静态代码块 3.子类的静态成员变量初始化 4.子类的静态代码块 5.父类的普通成员变量初始化 ...

  3. CodeForces600E Lomsat gelral 线段树合并

    从树上启发式合并搜出来的题 然而看着好像线段树合并就能解决??? 那么就用线段树合并解决吧 维护\(max, sum\)表示值域区间中的一个数出现次数的最大值以及所有众数的和即可 复杂度\(O(n \ ...

  4. Codeforces Round #295 (Div. 2)A - Pangram 水题

    A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  5. 【转 记录】python中的encode以及decode

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  6. 轨至轨运算放大器 rail to rail

    http://www.360doc.com/content/10/1102/16/2285160_66006645.shtml Rail to rail: 轨至轨,指器件的输入输出电压范围可以达到电源 ...

  7. mysql-debug

    http://www1.huachu.com.cn/read/readbookinfo.asp?sectionid=1000002778 http://hedengcheng.com/?p=238 h ...

  8. postgresql的ALTER经常使用操作

    postgresql版本号:psql (9.3.4) 1.添加一列ALTER TABLE table_name ADD column_name datatype; 2.删除一列 ALTER TABLE ...

  9. 取消SVN版本号控制的bash脚本

    原理非常easy,递归删除当前文件夹下全部的 .svn 文件. 把 .svn 换成 .git 就可以用于删除 git 控制

  10. java多线程知识点汇总(一)多线程基础

    1.什么叫多线程程序? 答:一个进程至少有一个线程在运行,当一个进程中出现多个线程时,就称这个应用程序是多线程应用程序. java编写的程序都是多线程的,因为最少有俩线程,main主线程和gc线程. ...