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. Swift2.0语言教程之类的嵌套与可选链接

    Swift2.0语言教程之类的嵌套与可选链接 Swift2.0语言类的嵌套 在一个类中可以嵌套一个或者多个类.它们的嵌套形式也是不同的,大致分为了两种:直接嵌套和多次嵌套.下面依次讲解这两种方式. S ...

  2. html5那些事儿

    一.优势1.标签的改变:<header>,<footer>,<dialog>,<aside>,<figure>,<section> ...

  3. Revit二次开发示例:ChangesMonitor

    在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息.   #region Namespaces using System; using System.Collections.Ge ...

  4. MySql 模糊查询、范围查询

    实例: SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下: 1.LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden). 2.LIKE'%inger' ...

  5. android 后台 activity 被系统回收 保存状态

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 活动被系统回收, 要保存状态 ,用到 活动的 在保存实例时候 的 这个方法. 当系统异常 ...

  6. 直接插入排序(初级版)之C++实现

    直接插入排序(初级版)之C++实现 一.源代码:InsertSortLow.cpp /*直接插入排序思想: 假设待排序的记录存放在数组R[1..n]中.初始时,R[1]自成1个有序区,无序区为R[2. ...

  7. BZOJ3589动态树

    **错误改了一上午. 先做熟练泼粪 k<=5,因此我们可以模拟这个过程,在线段树上把标记建出来然后pushup时候更新就好了. By:大奕哥 #include<bits/stdc++.h& ...

  8. 2018/3/20 noip模拟赛 5分

    T1 傻逼题,写了cmp没sort,5分. T2 树上差分,写了树剖线段树,超时,0分. T3 树归,0分. 我是个zz

  9. 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken

    题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ...

  10. 20162303 解读同伴的收获&解决同伴的问题 周三补交

    解读同伴的收获&解决同伴的问题 11月29号 解决同伴的问题 我的同组同学是20162307学号张韵琪同学 同组同学的问题是动态规划算法步骤中递归定义的最优值 我理解他的意思是她不太理解最优值 ...