Optimization Problem with a Constraint

In summary: I fixed it. In summary, this algorithm selects two adjacent houses to rob, assuming the constraint that no two houses can be robbed in a row.
  • #1
FallenApple
566
61

Homework Statement


This is a leetcode question.

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.

Homework Equations


The constraint is two houses cannot be robbed.

The Attempt at a Solution


Actually, this is a solution I found online.

Code:
public int rob(int[] num) {
   if(num==null || num.length == 0)
       return 0;

   int even = 0;
   int odd = 0;

   for (int i = 0; i < num.length; i++) {
       if (i % 2 == 0) {
           even += num[i];
           even = even > odd ? even : odd;
       } else {
           odd += num[i];
           odd = even > odd ? even : odd;
       }
   }

   return even > odd ? even : odd;
}

The code is in Java. I'm not sure why the even and odd "pointers" need to switch. How does this solve the problem? We know they cannot rob two houses in a row. But how does this relate to evens and odds? If what is considered even and odd constantly switches, then what is the purpose of giving them the names in the first place?
 
Physics news on Phys.org
  • #2
They don't switch. At each step, which corresponds to the next house in the row and wondering whether to rob it, there are always two totals, named odd and even. At house i, the odd (even) total is the biggest amount that can be obtained from robbing some collection of houses up to and possibly including the biggest odd (even) number <= i. Sometimes those two totals are the same, which is what comes out of the expressions 'even > odd ? even : odd'.

The difference between the even and odd totals is that, if i is odd (even), the odd (even) total encompasses the possibility (but not the necessity) of robbing house i, while the even (odd) total does not.

Consider the sequence of stashed sums 5 3 2.

At i=1, we have even=0, odd=5, because the odd pattern can rob house 1 but the even pattern can rob no houses.
At i=2, the even pattern could rob house 2 to get $3, but it is better to rob house 1 instead, as that nets a bigger amount and yet leaves open the possibility of robbing house 3 next, which robbing house 2 doesn't.

Now consider the pattern of stashed sums 3 5 7 2

At i = 1 we have even=0, odd=3
At i = 2 we have even=5, odd=3 because 5>3
At i = 3 we have even=5, odd=10 because 10>5
At i=4 we have even=10, odd=10 because robbing house 4 together with the existing even strategy nets a total of only 5+2=7, which is less than the 10 obtained from the odd strategy.

It's a neat solution.
 
  • Like
Likes FallenApple
  • #3
The only problem I have with this solution...

You'd think an additional constraint would be added limiting the number of houses one can rob per night. This solution assumes if you have 800 houses on the block you can rob half of them. It also assumes houses are only on one side of the street. Its possible that those assumptions are correct, but it seems too easy.
 
  • #4
It also fails for certain patterns. In the following pattern you would be better off going with house 2, 4, 7 then 2,4,6,8

1 20 1 20 1 1 20 1
 
  • #5
donpacino said:
It also fails for certain patterns. In the following pattern you would be better off going with house 2, 4, 7 then 2,4,6,8

1 20 1 20 1 1 20 1
The algorithm does choose 2 4 7 for that pattern. Here's the sequence:

Step; Odd Sequence; Even Sequence
1; 1; none
2; 1; 2
3; 2; 2
4; 2; 2 4
5; 2 4; 2 4
6; 2 4; 2 4 6
7; 2 4 7; 2 4 6
8; 2 4 7; 2 4 7
 
  • #6
ahhh true. For some reason I interpreted the code as a convoluted way to sum up the odd and even numbers
 

Related to Optimization Problem with a Constraint

1. What is an optimization problem with a constraint?

An optimization problem with a constraint involves finding the maximum or minimum value of a function while also satisfying a specific condition or limitation.

2. How is an optimization problem with a constraint solved?

Optimization problems with constraints can be solved using various methods such as the Lagrange multiplier method, the KKT conditions, or the simplex algorithm. The appropriate method depends on the specific problem and constraints involved.

3. What is the purpose of a constraint in an optimization problem?

The purpose of a constraint in an optimization problem is to represent real-world limitations or conditions that must be considered in finding the optimal solution. Constraints help to make the solution more practical and realistic.

4. Can an optimization problem have multiple constraints?

Yes, an optimization problem can have multiple constraints. In fact, most real-world problems involve multiple constraints that must be satisfied in order to find the optimal solution.

5. How does the addition of a constraint affect the solution of an optimization problem?

The addition of a constraint can significantly affect the solution of an optimization problem. It can restrict the possible solutions and change the optimal value, making it more challenging to find the optimal solution. However, constraints are necessary for making the solution more realistic and applicable to real-world situations.

Similar threads

  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
929
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
32
Views
934
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top