Solution to House Robber II by LeetCode

3 Feb


Question Name: House Robber II

This is a variant of House Robber. We need to apply the previous solution to two sub-arrays and return the maximum of the two results:

1. Do not use the first element, and we can safely consider (may or may not use) the last element (case #1);

2. Oppositely, consider (may or may not use) the first element, and ignore the last element. If the first item was used (case #2a), it is reasonable to ignore the last item. Otherwise (case #2b), case #2b and case #1 share the same leading elements. But case #1 has one more element at the end, therefore case #1 always has an equal-or-larger result.

Totally we need the max(case #1, case #2a, case #2b) = max(case #1, case #2).

2 Replies to “Solution to House Robber II by LeetCode

  1. Just want to stop by and say hi ^_^

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!