6.House Robber(简单版抢银行)
Level:
Easy
题目描述:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
思路分析:
可以用动态规划的思想去解决这个问题,由题意知,抢银行不能抢相邻的,所以我们用dp[i]表示有前i个银行时,所能抢到的最多钱,那么状态转移方程很容易可以得出:dp[i]=max(dp[i-2]+num,dp[i-1]),其中num指的是第i个银行里钱的数目。
代码:
class Solution {
public int rob(int[] nums) {
if(nums.length==0||nums==null)
return 0;
int []dp=new int[nums.length+1];
dp[0]=0; //数组中前0个元素时抢到的最多钱
dp[1]=nums[0];//数组中前1个元素时抢到的最多钱
for(int i=2;i<=nums.length;i++){
dp[i]=Math.max(dp[i-2]+nums[i-1],dp[i-1]);
}
return dp[nums.length];
}
}
6.House Robber(简单版抢银行)的更多相关文章
- JavaMail简单版实验测试
前言: 最近由于实现web商城的自动发送邮件功能的需求,故涉猎的邮箱协议的内部原理.现将简单版的Java Mail实例做个代码展示,并附上其中可能出现的bug贴出,方便感兴趣的读者进行测试! 1.载入 ...
- 小米抢购(简单版v0.1)-登录并验证抢购权限,以及获取真实抢购地址
小米(简单版)-登录并验证抢购权限,以及获取真实抢购地址! 并不是复制到浏览器就行了的 还得传递所需要的参数 这里只是前部分 后面的自己发挥了 { "stime": 1389 ...
- Java实现简单版SVM
Java实现简单版SVM 近期的图像分类工作要用到latent svm,为了更加深入了解svm,自己动手实现一个简单版的. 之所以说是简单版,由于没实用到拉格朗日,对偶,核函数等等.而 ...
- MySQL数据库执行计划(简单版)
+++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库执行计划简单版时间:2019年2月25日内容:MySQL数据库执行计划简单版重点:MySQL ...
- 红警大战JAVA简单版
代码结构: 相关源码: 武器类: 属性:武器,攻击力,子弹数量. 方法:给属性赋值(set属性()方法) 获取属性值(get属性()方法) package 红警大战简单版; public class ...
- TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇
TOJ3973传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3973 时间限制(普通 ...
- 模板】AC自动机(简单版)
模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P3808 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保 ...
- PAT 1089 狼人杀-简单版(20 分)(代码+测试点分析)
1089 狼人杀-简单版(20 分) 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局"狼人杀"游戏中,1 号玩家 ...
- springmvc springJDBC 简单实训银行账户管理系统
springmvc springJDBC 简单实训银行账户管理系统 1.简单介绍一下,在校时每周结束都会有一次学习总结,简称“实训”,这次实训内容是spring,因为是最近热门框架,我就先从基础方面开 ...
随机推荐
- web.config中namespace的配置(针对页面中引用)
1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...
- 触摸事件MultiTouch Events
备注: userInteractionEnabled = NO hidden = YES alpha = 0.0~0.01 //如果上面三个属性被设置了则无法接收触摸事件 1.- (void)t ...
- 链接ssh失败问题
Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable. ...
- python取一个字符串中最多出现次数的词
#-*- coding:utf-8 -*- #取一个字符串中最多出现次数的词 import re from collections import Counter my_str = "&quo ...
- 重命名File
File completeFile = new File(mFilePath + mFileName); if (completeFile.exists()) { File fileWithSuffi ...
- 关于c#分支语句和分支嵌套还有变量的作用域。
分支语句: if....else if....else 必须以 if 开头 后面加括号写入需要判断的内容. 举个栗子说明一下 if (bool类型(比较表达式)) // 他会判断括号内的条件是否 ...
- 【摘自张宴的"实战:Nginx"】http auth baseic模块(打开页面需要密码验证)
location /admin { auth_basic "kingsoft"; auth_basic_user_file httppasswd; #密码文件的路径 默 ...
- HDOJ 1023 Train Problem II 卡特兰数
火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...
- input框去只读
设置为只读 $('input[name=video_link]').attr("readonly","readonly"); 去只读 $("#vide ...
- javax.swing.Timer
javax.swing 类 Timer java.lang.Object javax.swing.Timer 所有已实现的接口: Serializable public class Timerexte ...