动态规划 - 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 ...
随机推荐
- Error[Pe020]: identifier "FILE" is undefined
Error[Pe020]: identifier "FILE" is undefined 需要添加头文件:#include <stdio.h>
- File的创建
package cn.lijun.demo3; import java.io.File; import java.io.IOException; // 创建文件功能 如果文件已经存在 不再创建 pub ...
- mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY
mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY 今天开发的同事发来如下错误信息,最最简 ...
- Maven Tomcat7+ 实现自动化部署
首先在Tomcat里配置deploy的用户(tomcat根目录/conf/tomcat-users.xml): <role rolename="tomcat"/> &l ...
- 神经网络4_BP神经网络
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- ansible-playbook 单个yml文件部署tomcat简单示例
#单yaml配置[root@jenkins pb]# cat tomcat.yml --- - hosts: eee vars: #设置变量 war_files: /var/lib/jenkins/w ...
- java io系列25之 PrintWriter (字符打印输出流)
更多内容请参考:java io系列01之 "目录" PrintWriter 介绍 PrintWriter 是字符类型的打印输出流,它继承于Writer.PrintStream 用于 ...
- python while 格式化 运算符 编码
#######################总结############# 1. 循环 while 条件: 循环体(break, continue) 循环的执行过程: 执行到while的时候. 首先 ...
- [leetcode-108,109] 将有序数组转换为二叉搜索树
109. 有序链表转换二叉搜索树 Given a singly linked list where elements are sorted in ascending order, convert it ...
- PowerDesigner设置一对一关系
(1)修改Cardinalities 为One-one (2)设置Dominant role A->B(假设表A,表B),保存 (3)到Joins页,设置Parent为None,设置Parent ...