house robber

来源:互联网 发布:vs2010 编译php扩展 编辑:程序博客网 时间:2024/05/21 17:33

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.



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);}


前一天抢了,今天就不能抢,前一天没抢,今天可以抢

动态规划

0 0
原创粉丝点击