Leetcode 198: House Robber
来源:互联网 发布:淘宝联盟的导购推广位 编辑:程序博客网 时间:2024/04/28 05:09
House Robber
Total Accepted: 642 Total Submissions: 2562
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.
[分析]
维持两个数组, 一个是包含最后一个字符的最大值, 一个是不包含最后一个字符的最大值. 更新两个数组, 最后求 max(d[n-1], b[n-1])即可.
public class Solution { public int rob(int[] num) { // 31 23 9 13 49 1 0 // 0 0 0 0 if(num==null || num.length==0) return 0; int n = num.length; int [] b = new int[n]; //include last element; int [] d = new int[n]; //exclude last element; b[0] = num[0]; d[0] = 0; for(int i=1; i<n; i++) { b[i] = d[i-1] + num[i]; d[i] = Math.max(b[i-1], d[i-1]); } return Math.max(d[n-1], b[n-1]); }}
0 0
- Leetcode 198: House Robber
- leetcode-198 House Robber
- [LeetCode 198]House Robber
- House Robber - LeetCode 198
- leetcode 198 House Robber
- leetCode #198 House Robber
- LeetCode 198-House Robber
- Leetcode[198]-House Robber
- 【Leetcode】House Robber #198
- Leetcode 198 House Robber
- leetcode 198:House Robber
- House Robber (leetcode 198)
- 【LeetCode】198:House Robber
- leetcode[198]:House Robber
- leetcode-198-House Robber
- leetcode 198: House Robber
- [LeetCode 198]House Robber
- Leetcode# 198 House Robber
- hihoCoder_二分·归并排序之逆序对
- LeetCode - Single Number
- [Leetcode] 85. Maximal Rectangle
- HTML 超链接相关内容
- #AOS应用基础平台# 实现了在用户权限范围内自定义的快捷菜单的导航展示
- Leetcode 198: House Robber
- [Leetcode] 86. Partition List
- Reverse Words in a String ---leetcode 我的java题解
- Sublime Text3打造U盘便携Lua IDE
- UIAlertView点击确定后,如何跳转到storyboard中创建的另一个UIViewController?
- 黑马程序员-JAVASE入门(GUI类)
- lua实现大数运算
- 画线算法 - Bresenham原理及Java实现
- [Leetcode] 87. Scramble String