Leetcode - Arrays | House Robber
198. House Robber[E]
https://leetcode.com/problems/house-robber/
Description
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:
1 | Input: nums = [1,2,3,1] |
Example 2:
1 | Input: nums = [2,7,9,3,1] |
Constraints:
0 <= nums.length <= 1000 <= nums[i] <= 400
Solution
https://leetcode.com/problems/contains-duplicate/solution/
1 | class Solution: |
213. House Robber II[M]
https://leetcode.com/problems/house-robber-ii/
Description
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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:
1 | Input: [2,3,2] |
Example 2:
1 | Input: [1,2,3,1] |
Solution
https://leetcode.com/problems/house-robber-ii/solution/
1 |
337. House Robber III[M]
https://leetcode.com/problems/house-robber-iii/
Description
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
1 | Input: [3,2,3,null,3,null,1] |
Example 2:
1 | Input: [3,4,5,1,3,null,1] |
Solution
1 |





