- 33.4%

https://leetcode.com/problems/house-robber-ii/#/description

Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

方法一：

与house robber i 类似，但是这个可以看做两个，一个是nums[2:],一个是nums[:-1]这两个偷盗情况。这样就可以随意像一个那么偷了。

https://discuss.leetcode.com/topic/14504/9-lines-0ms-o-1-space-c-solution

9-lines 0ms O(1)-Space C++ solution

This problem is a little tricky at first glance. However, if you have finished the House Robber problem, this problem can simply be decomposed into two House Robber problems.

Suppose there are n houses, since house 0 and n - 1 are now neighbors, we cannot rob them together and thus the solution is now the maximum of

- Rob houses 0 to n - 2;
- Rob houses 1 to n - 1.

The code is as follows. Some edge cases (n < 2) are handled explicitly.

1 | class Solution { |

我的代码实现：

1 | class Solution { |

https://discuss.leetcode.com/topic/14325/twice-pass-solution-c

Twice pass solution, C++

Twice pass:

not rob nums[n-1]

not rob nums[0]

and the other is same as House Robber.

1 | int rob(vector<int>& nums) |

https://discuss.leetcode.com/topic/20770/c-super-simple-0ms-solution-with-explanation

[C++] Super Simple 0ms solution with explanation

Since you cannot rob both the first and last house, just create two separate vectors, one excluding the first house, and another excluding the last house. The best solution generated from these two vectors using the original House Robber DP algorithm is the optimal one.

1 | class Solution { |

https://discuss.leetcode.com/topic/18123/0ms-o-n-time-o-1-space-c-solution

0ms O(N) time O(1) space C++ solution

This solution is based on house robber 1. The idea is that either the first house or the last house is not robbed. The final solution is max of (house robber without last element) and (house robber without the first element). Note endIndex is not inclusive in the second rob function.

1 | class Solution { |

Simple and easy C++ solution modified from the best solution of House Robber (Easy)

1 | int rob_line(vector<int>& nums, int start, int end) { |

1 | # Solution mine: |