- Denisse's Newsletter
- Posts
- Its a robbery not to open this đ
Its a robbery not to open this đ
198: House Robber

Drowning In Support Tickets? Maven AGI is here to help.
Maven AGI platform simplifies customer service by unifying systems, improving with every interaction, and automating up to 93% of responses. Seamlessly integrated with 50+ tools like Salesforce, Freshdesk, and Zendesk, Maven can deploy AI agents across multiple channelsâtext, email, web, voice, and appsâwithin days. Companies like Tripadvisor, ClickUp, and Rho slash response times by 60%, ensuring quicker support and exceptional customer satisfaction. Donât let support tickets slow you down
TLDR
Welcome!
198. House Robber
Recommendations I live by!
Resources to check out
Let me invoke you into my cult
JOBS & SCHOLARSHIPS
Hi and welcome back, brave human!
How has your week been so far?
Before we dive into this, I want to remind you something.
The reason why we say people are geniuses and that it could never be us gives us a reason to be lazy.
It gives us a reason not to try; it even gives us space to do nothing at all.
But you and I, we know that every genius once struggled, and you and I are geniuses in the making!
LFG!!
What type of Content would you like to see? |
LeetCode problem
Day 2 : 198 House Robber
You're a robber deciding which houses to rob along a street, where each house has a certain amount of money.
To avoid triggering security alarms, you can't rob two adjacent houses; given an array `nums` of house values, calculate the maximum money you can steal without alerting the police.
Before we get started let's take a deep breathâŚThis is a warm-up exercise for the year to come!!!
It took me some good time to wrap my head around this problem, so letâs take it step by step!
What are we asked? Find the max amount of money we can rob without getting caught! đ¤
What do we need to calculate each step? For each step, we need to maximize for gains, does it make more sense to rob 2 houses over? or the house adjecent?
Base cases:
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1]
Let's look at an example with [2,7,9,3,1]
.
The sequence builds up like this:
dp[0] = 2
dp[1] = 7
dp[2] = cost[2] + max((dp[0]+nums[2]), dp[1])
We will use dynamic programming to solve this problem. This problem is a combination of coin change and climbing stairs. If you need a refresher on dynamic programming.
The key difference is we'll be maximizing profit instead of minimizing cost, and handling the constraint of not robbing adjacent houses. We'll build iteratively from the base cases:
We start by initializing an array dp
where dp[i]
represents the maximum amount we can rob up to house i. We'll set:
dp[0] = nums[0]
(rob first house)dp[1] = max(nums[0], nums[1])
(maximum of first two houses)
Then we build the array up đď¸, solving one house at a time from 2 up to n. For each house i:
We consider both options at house i:
Skip this house (take previous best)
Rob this house (take best from two houses back + current house)
Keep track of the maximum result found
To track the maximum result at each house:
dp[i]
- current best (maximum) amount we can rob up to house idp[i-1]
- maximum if we skip current housedp[i-2]
- maximum if we rob current house (must add nums[i])nums[i]
- amount in current housedp[i-2] + nums[i]
- adds current house to best from two backmax(dp[i-1], dp[i-2] + nums[i])
- keeps maximum between skipping or robbing
This gives us our final DP transition: dp[i] = max(dp[i-1], dp[i-2] + nums[i])
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 1) {
return nums[0];
}
if (nums.size() == 2) {
return max(nums[0], nums[1]);
}
vector<int> dp(nums.size());
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for(int x = 2; x < nums.size(); x++) {
dp[x] = max(dp[x-2] + nums[x], dp[x-1]);
}
return dp[nums.size()-1];
}
};
What is the O Notation? |

Recommendations
Level up your inbox by subscribing to what I am subscribed to!
Let me invoke you into my cult
Media worth seeing
3blue1brown is the best Math channel I have found! He just released a LLMs series and it is the best explanation in the internet has of how LLMs work
Hacks of the week
I truly believe you can gaslight yourself to success.
The shorter the time you give yourself to take action the more likely youâll be successful. I read a tweet on Friday about a guy who was walking a marathon each week and I decided to walk one the very next day (evidence attached!!)
Jobs and Scholarships
Cheers to you hacking your week!
Denisse
Reply