Member-only story

House Robber

Nishant Tanwar 🕉️
3 min readMar 27, 2021

This is the solution for House Robber Problem on Leetcode.

Problem 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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Solution

Recursive Approach :

Our approach here will be to try to go over all possibilities of maximizing the loot for the thief. To go over all possibilities we are limited by essentially two choices, do we decide to rob the current index house and ignore the next one and directly jump over to the house which is index + 2 from us, or do we ignore the current house and jump over to the next house by index+1 and rob that house. We execute both the choices and in the end pick the choice with maximum loot and return that as the answer.

Our exit condition here to come out of the recursion will be if the index is equal to or greater(if we are doing index+2 this situation…

--

--

Nishant Tanwar 🕉️
Nishant Tanwar 🕉️

Written by Nishant Tanwar 🕉️

Software Engineer @ Google. I write about programming, system design and health. Follow me for resources to crack your dream company.

No responses yet