house robber problem how to do this this way

house robber problem how to do this this way

house robber ii
house robber python
house robber iii geeksforgeeks
house robber memoization
house robber test cases

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.
class Solution {
public int rob(int[] nums) {
    int sim=0;
    int sum=0;
    int i,j;

    for(i=0;i<nums.length;i++,i++){
        sim+=nums[i];
    }
    for(j=1;j<nums.length;j++,j++){
        sum+=nums[j];
    }
    int r= Math.max(sim,sum);
    return r;
}
}

How to do this logic when array length is in odd ? can we do that this way output is correct for even length though


Your solution is skipping one house after robbing previous one. This would not always give maximum output. Consider this case: [100, 1, 1, 100]. According to your solution, sim == 101 and sum == 101, however, the correct solution would be 200. (robbing the 0th and 3rd house).

I propose two possible solutions: 1. using recursion, 2. using dp.

Using recursion, you can choose either to rob a house and skip next one, or do not rob a house and go on to the next one. Thus, you will have two recursive cases which would result in O(2^n) time complexity and O(n) space complexity.

public int rob(int[] nums) {
    return robHelper(nums, 0, 0);
}

private int robHelper(int[] nums, int ind, int money) {
    if (ind >= nums.length) return money;

    int rec1 = robHelper(nums, ind+1, money);
    int rec2 = robHelper(nums, ind+2, money+nums[ind]);
    return Math.max(rec1, rec2);
}

Using dp would optimize time and space complexity from above solution. You can keep track of two values: currMax and prevMax. While prevMax is max money excluding the previous house, currMax is max money considering the previous house. Since prevMax is guaranteed that money from previous house is not included, you can add money from current house to prevMax and compare it with currMax to find total max money up to that point. Here is my solution using dp, O(n) time complexity and O(1) space complexity:

public int rob(int[] nums) {
    int currmax = 0;
    int prevmax = 0;

    for (int i = 0; i < nums.length; i++) {
        int iSum = prevmax + nums[i];
        prevmax = currmax;
        currmax = Math.max(currmax, iSum);
    }
    return currmax;
}

How to Solve the House Robber Problem - Outco, The House Robber Problem is popular in software engineering house to come up with our plan, it's difficult to imagine a way of doing this  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]


Solving House Robber Algorithm #DynamicProgramming, Dynamic Programming is a computer programming method to optimize recursion. It breaks down a complicated problem into simpler sub-problems so We will compare the amount the robber can steal from the first house  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.


private static int rob(int[] money) {
    int max = 0;

    for (int i = 0; i < money.length; i++) {
        int skips = 2;

        while (skips < money.length) {
            int sum = 0;

            for (int j = 0; j < money.length; j += skips) {
                sum += money[j];
            }

            if (sum > max) {
                max = sum;
            }
            skips++;
        }
    }

    return max;
}

GOOGLE CODING INTERVIEW QUESTION, Coding Interviews House Robber (LeetCode) question and explanation. amount of money we Duration: 7:32 Posted: Mar 27, 2019 If a house looks vacant and unprepared, that house may be a victim of a robbery. Leaving lights on is a safe measure for any house. This can be done by setting up a timer to your lights to turn them on at specific times. Set them to realistic periods, such as in the evening, and not very late at night. Doing this will prevent suspicion.


Find maximum possible stolen value from houses, A thief is going to steal the maximal value of these houses, but he can't steal in two and converting the recursive solution to Dynamic programming problem. The solution to this dilemma is to avoid passing the running value (money you've robbed) downward through the recursive calls from parents to children. The idea is to compute sub-problem results without any input from ancestor nodes, then build the larger solutions from the smaller ones on the way back up the call stack.


House Robber, 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  Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved.


A graphical introduction to dynamic programming, One way to avoid re-calculating the same subproblems again and Second, we can see the digram is a directed acyclic graph (or DAG for short), meaning: In the House Robber Problem, you are a robber who has found a  It usually involves deducing some fact based on the constraints of the problem that helps you look at it in a different way. For example, in the House Robber problem, you can’t rob adjacent houses,