Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
House Robber
MEDIUM
35 mins
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Dynamic Programming
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

House Robber

Contributed by
Ratnesh
Medium
yellow-spark
0/80
Avg time to solve 35 mins
Success Rate 80 %
Share
0 upvotes

Problem Statement

Mr. X. is a professional robber planning to rob houses along a street. Each house has a certain amount of money hidden.

Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two neighboring houses were broken into the same night.

You are given an array/list of non-negative integers 'A' representing the amount of money hidden at each house. Your task is to return the maximum amount of money Mr. X can rob tonight without alerting the police.

Example:
Input: [1,2,3,1]

Output: 4

Mr. X. can rob the first house and the third house, total money = 1 + 3.
Hence, the total money robbed by Mr. X. is 4.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
4   
4 1 6 10
5
2 5 8 9 1
Sample Output 1 :
14
14
Explanation Of Sample Input 1 :
For the first case:

Input: [4,1,6,10]

Output: 4

Mr. X. can rob the first house and the fourth house, total money = 4 + 10.
Hence, the total money robbed by Mr. X. is 14.

For the second case:

Input: [2,5,8,9,1]

Output: 14

Mr. X. can rob the second and fourth houses, total money = 5 + 9.
Hence, the total money robbed by Mr. X. is 14.
Sample Input 2 :
2
8
6 1 50 1 50 30 6 100
6
55 19 21 13 10 67
Sample Output 2 :
206
143
Reset Code
Full screen
Auto
copy-code
Console