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. 总结html

    1.初识html W3C : 万维网联盟!(World Wide Web Consortium )   创建于1994年,是web技术领域最权威最具有影响力的标准机构!           W3C规定 ...

  2. 项目Alpha冲刺——代码规范、本次冲刺任务与计划

    作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称: 那周余嘉熊掌将得队 作业目标:代码规范.本次冲刺任务与计划 团队信息: 队员学号 队员姓名 博客 ...

  3. 图形文件元数据管理工具exiv2

    图形文件元数据管理工具exiv2   图形文件通常都包含多种元数据,如Exif.IPTC.XMP.这些信息往往是渗透人员收集的目标.为了便于管理这些信息,Kali Linux内置了专用工具exiv2. ...

  4. 1009 Product of Polynomials (25)(25 point(s))

    problem This time, you are supposed to find A*B where A and B are two polynomials. Input Specificati ...

  5. [Agc002E]Candy Piles

    [Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...

  6. Python知识(1)----基础入门和进阶总结。

    今天把Python的语法过了一遍,学习了慕课网上的教程,简单易懂,1个小时就可以入门Python了.Python有两个主要的版本,Python2.7,Python3.5,后面的版本,改动较大,编写的程 ...

  7. ISO 7816-4: Interindustry Commands for Interchange

    5. Basic Organizations 5.1 Data structures5.2 Security architecture of the card 5.3 APDU message str ...

  8. SonarQube使用

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,下面将会介绍一下这个工具的安装.配置以及使用. 一.安装 1.下载好sonarqube后,解压打开bin目录,启 ...

  9. jsp中简易版本的图片上传程序

    1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO   ...

  10. struts2点滴记录

    1.s:textfield 赋值方法 <s:textfield name="Tname" value="%{#session.Teacher.name}" ...