198. House Robber Java Solutions

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.

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public int rob(int[] nums) {
 3         if(null == nums || nums.length == 0) return 0;
 4         if(nums.length == 1) return nums[0];
 5         //if(nums.length == 2) return nums[0] > nums[1] ? nums[0] : nums[1];
 6         int[] res = new int[nums.length];
 7
 8         res[0] = nums[0];
 9         res[0] = Math.max(nums[0] , nums[1]);
10         for(int i = 2;i < res.length; i++){
11             res[i] = Math.max(res[i-2]+nums[i],res[i-1]);
12         }
13         return res[res.length-1];
14     }
15 }
时间: 2024-11-13 20:40:26

198. House Robber Java Solutions的相关文章

[LeetCode] 198. House Robber Java

题目: 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 a

leetCode 198. House Robber | 动态规划

198. 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

47. Permutations II java solutions

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

119. Pascal&#39;s Triangle II java solutions

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

54. Spiral Matrix java solutions

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. Subscribe to see which c

刷题198. House Robber

一.题目说明 题目198. House Robber,给一列正整数表示每个房间存的金币,不能连续抢2个房间,计算可以得到的最大金币. 二.我的解答 这个题目,我列举了n=1,2,3,...5的情况,没有找到规律.后面看了解答知道了: dp[i+1]= max(dp[i-2]+nums[i],dp[i-1]) 代码如下: class Solution{ public: int dfs(vector<int>& nums,int n){ if(n==0) return 0; if(n==1

Java for LeetCode 198 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 autom