动态规划 - 198. House Robber
URL : https://leetcode.com/problems/house-robber/
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.
解决方案
/**
* 罗列出每天偷Y和不偷N的盈利情况
* Y(n) = nums[n] + N(n-1); //前一天肯定不能偷,再加上今天偷的金额
* N(n) = Max(Y(n-1), N(n-1)); //前一天偷或者不偷均可,选取最大值。因为今天不再偷,所以不必加另外的金额
* 最后的结果就是Max(Y(n),N(n))...
*/ import java.lang.Math;
class Solution {
public int rob(int[] nums) { if(nums == null || nums.length < 1){
return 0;
} int pre_yes = 0;
int pre_no = 0; int cur_yes = 0;
int cur_no = 0; for(int i = 0; i < nums.length; ++i){
cur_yes = nums[i] + pre_no;
cur_no = Math.max(pre_yes,pre_no); pre_yes = cur_yes;
pre_no = cur_no;
}
return Math.max(pre_yes,pre_no); }
}
动态规划 - 198. House Robber的更多相关文章
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- Leetcode 198 House Robber 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...
- 198. House Robber(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 一道简单的动态规划题目——House Robber
一.题目 House Robber(一道Leetcode上的关于动态规划的简单题目)具体描述如下: There is a professional robber planning to rob hou ...
随机推荐
- 学习Docker之Dockerfile的命令
使用Dockerfile去构建镜像好比堆积木.使用pom去构建maven项目一样,有异曲同工之妙,下面就把Dockerfile中主要的命令介绍一下. 组成部分 部分 命令 基础镜像信息 FROM 维护 ...
- WCF博文链接
我的基于WCF的SOA架构项目实战 http://www.uml.org.cn/soa/201112201.asp WCF实战(一):创建服务器类 https://blog.csdn.net/qium ...
- 删除mysql数据库中表分区数据
删除mysql数据库中表分区数据 zabbix 几个大表创建了分区,由于磁盘空间告警,特将3月前的分区给予删除. 1.查看表的数据占用磁盘空间情况 2.登录mysql中,查看表的分区情况. 3.删除表 ...
- MyBatis-resultType 几种返回类型
一.返回集合 1.返回JavaBean集合 public List<MyUser> selectMyUserByNameLike(String name); <!-- resultT ...
- Tomcat、Weblogic、WebSphere、JBoss服务器的选择
一.应用点 Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键技术,所以用户可以用Tomcat开发基于数据库,Servlet和JSP页面的 ...
- sc (service control )
SC 是用来与服务控制管理器和服务进行通信 net: net start 服务名 net stop 服务名 sc: sc config 服务名 start= demand //手动 sc con ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- Basic berkeley socket functions
gethostbyname() DNS を通して.Domain の Information を GET する.例えば IP Address なんだ. げん型: #include <netdb.h ...
- jqweui Picker使用一个小问题
地址:http://jqweui.com/extends#picker加了Display Value后,会产生改变值后,Picker显示Value而不显示Text情况.需要在OnClose里做如下处理 ...
- Newtonsoft.Json序列化Enum类型
[JsonConverter(typeof(StringEnumConverter))] public StringAlignment TextAlign { get => textAlign; ...