Leetcode 198: House Robber

grid47
grid47
Exploring patterns and algorithms
Oct 18, 2024 5 min read

A series of houses softly glowing, with the optimal path for robbing illuminating.
Solution to LeetCode 198: House Robber Problem

You are a burglar planning to rob houses along a street. Each house has an amount of money, but robbing two adjacent houses will trigger an alarm. Given an integer array nums representing the money stashed in each house, find the maximum amount you can rob without triggering the alarm.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of integers where each integer represents the amount of money in each house.
Example: nums = [5, 3, 4, 11]
Constraints:
• 1 <= nums.length <= 100
• 0 <= nums[i] <= 400
Output: The output is the maximum amount of money that can be robbed without triggering the alarm by robbing adjacent houses.
Example: Output = 16
Constraints:
• The output is a single integer representing the maximum amount of money that can be robbed.
Goal: Maximize the total amount of money robbed while ensuring no two adjacent houses are robbed.
Steps:
• Step 1: Use dynamic programming to keep track of the maximum money robbed up to each house.
• Step 2: For each house, calculate the maximum amount by either skipping the house or robbing it and adding its value to the previous non-adjacent house's value.
• Step 3: Return the maximum value at the last house.
Goal: The problem constraints ensure that the input list contains valid house values and its length is manageable.
Steps:
• The length of the input list, `nums`, is between 1 and 100.
• Each value in `nums` is between 0 and 400.
Assumptions:
• The input array is non-empty.
• The values in the input array are valid integers within the given range.
Input: Input: nums = [5, 3, 4, 11]
Explanation: The maximum money that can be robbed is 16 by robbing house 1 (5) and house 4 (11). The adjacent houses can't both be robbed, so we skip house 2 and 3.

Input: Input: nums = [3, 2, 5, 10, 7]
Explanation: The optimal choice is to rob house 1 (3), house 3 (5), and house 5 (7), yielding a total of 15.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus