🔥 The ultimate robbery 🔥

And how to turn your anger into creativity.

In partnership with

Learn AI in 5 minutes a day

What’s the secret to staying ahead of the curve in the world of AI? Information. Luckily, you can join 1,000,000+ early adopters reading The Rundown AI — the free newsletter that makes you smarter on AI with just a 5-minute read per day.

TLDR

  1. Welcome!

  2. 337. House Robber III

  3. Recommendations I live by!

  4. Resources to check out  

  5. Let me invoke you into my cult

  6. JOBS & SCHOLARSHIPS

Hi and welcome back, brave human!

This week’s problem took me a complete Sunday to solve.

And what kept me from solving this problem was my inner critic.

“Why can’t you solve this problem? You have a newsletter dedicated to LeetCode, you should be able to do this in your sleep” 

And you see, that is why it took me so long to solve.

I was not allowing my brain to think, at all. 

And you my friend are having the same issue when doing these m!”# problems.

You get frustrated for not knowing the answer in a millisecond.

So you don’t allow yourself to think.

It takes longer, you get more frustrated, and it’s god damn cycle.

Breeeeeeeeathe………..

Allow yourself to be a beginner. 

Break the problem into easier problems, and take as long as you want. 

Only then, will you be able to rationalize these problems.

Let’s F%$! Go!!!

There’s a reason 400,000 professionals read this daily.

Join The AI Report, trusted by 400,000+ professionals at Google, Microsoft, and OpenAI. Get daily insights, tools, and strategies to master practical AI skills that drive results.

LeetCode problem

Day 8: House Robber III

Given the root of a binary tree where each node represents a house with money, return the maximum amount the thief can rob without robbing two directly connected houses. 

Before we get started let's take a deep breath…This is f%$! hard but it’s worth doing if you want to pursue a career in tech.

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 maximum amount of money a thief can rob from houses arranged in a binary tree, ensuring that no two directly linked houses are robbed on the same night.

  • If you rob a house, its directly connected parent and child houses cannot be robbed, so you need to carefully choose which houses to rob to maximize the total amount stolen.

Now before we continue, there is an important realization you’ll make when doing this problem. You can’t discriminate an entire level of nodes just because the test problems hint at that answer.

Here is how we visualize the problem:

Intuition → House Robber

There are 2 possibilities:

1. To Rob 
2. Not to Rob 

If we rob:

1. We Add the value to our sum 
2. We skip our children 

If we don’t rob:

1. We can decide to rob 
2. We can decide to skip 

And how will we know what to choose?

We calculate both! And get the max between both.

We will do that by allowing our function to return an array with the structure

{Rob, Not Rob}

We will leave everything to recursion to do its 🪄 

We calculate the left and right trees with both possibilities

 pair<int,int>left = helper(root->left); 
 pair<int,int>right = helper(root->right);

and now we compare!

What is the O notation?

Login or Subscribe to participate in polls.

 pair<int,int> helper(TreeNode* root ){
        if(root==NULL){
            return {0,0};
            //Rob , Not Rob
        }
        pair<int,int>left = helper(root->left); 
        pair<int,int>right = helper(root->right);
        int robThis = root->val + left.second + right.second; 
        int notRob  = max(left.first, left.second) + max(right.first, right.second); 
        return {robThis, notRob};
            
        }
        

    int rob(TreeNode* root) {
        if(root==NULL){
            return 0; 
        }
        pair<int,int> answer = helper(root); 
        return max(answer.first, answer.second);
         
    }
};

Recommendations

Level up your inbox by subscribing to what I am subscribed to!

Let me invoke you into my cult

Media worth seeing

  1. Mira Murati just created the first AI company run by adults apply here

  2. Don’t blame me if you start seeing hearts everywhere, how to rewire your brain to find opportunities

  3. How to turn your dark energy into charisma

Cheers to you hacking your week!

Denisse

Reply

or to participate.