198. House Robber

来源:互联网 发布:儿童编程 ipad 编辑:程序博客网 时间:2024/06/13 09:59

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 andit 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 tonightwithout 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.


官方解答:

public int rob(int[] num) {    int[][] dp = new int[num.length + 1][2];    for (int i = 1; i <= num.length; i++) {        dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);        dp[i][1] = num[i - 1] + dp[i - 1][0];    }    return Math.max(dp[num.length][0], dp[num.length][1]);}

dp[i][1] means we rob the current house and dp[i][0] means we don't,

so it is easy to convert this to O(1) space

public int rob(int[] num) {    int prevNo = 0;    int prevYes = 0;    for (int n : num) {        int temp = prevNo;        prevNo = Math.max(prevNo, prevYes);        prevYes = n + temp;    }    return Math.max(prevNo, prevYes);}

原创粉丝点击