LeetCode: House Robber III

https://leetcode.com/problems/house-robber-iii/, problem statement:

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
 3
 / \
 2 3
 \ \ 
 3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
 3
 / \
 4 5
 / \ \ 
 1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.

Key idea is to do a DFS (Depth-First-Search) but using post-order traversal (left-right-current). Whenever the current node can be robbed, it will be robbed, but regardless we also need to try with the current node not being robbed (think about an extrapolated tree such as 1000->1(R)->1(R)->1000(R). In this case the best solution is 2000). We should also try from the beginning the root being robable and with it being not robable. This solution will be exponential and will eventually time out for long inputs. An DP (Dynamic Programming) like approach comes handy here: keep a hash table with the results seen for the current node and the current rob state, and whenever you see that situation, pull the result out of the hash table. This will speed up the code significantly, or at least enough to be accepted. Cheeers, Marcelo.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace LeetCode
{
class Program
{
static void Main(string[] args)
{
TreeNode tree = new TreeNode(3);
tree.left = new TreeNode(2);
tree.right = new TreeNode(3);
tree.left.right = new TreeNode(3);
tree.right.right = new TreeNode(1);

Solution sol = new Solution();
Console.WriteLine(sol.Rob(tree));
}
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}

public class Solution
{
public int Rob(TreeNode root)
{
if (root == null) return 0;

int maxAmount = Int32.MinValue;
Hashtable htMap = new Hashtable();
MaxRob(root, true, htMap, ref maxAmount);
MaxRob(root, false, htMap, ref maxAmount);

return maxAmount;
}

private void MaxRob(TreeNode currentNode,
bool canRob,
Hashtable htMap,
ref int maxAmount)
{
if (currentNode == null)
{
return;
}

string key = currentNode.GetHashCode().ToString() + "@" + canRob.ToString();
if (htMap.ContainsKey(key))
{
maxAmount = (int)htMap[key];
return;
}

int maxAmountLeft = 0;
int maxAmountRight = 0;

if (canRob)
{
//Will rob current
MaxRob(currentNode.left, false, htMap, ref maxAmountLeft);
MaxRob(currentNode.right, false, htMap, ref maxAmountRight);

if (maxAmountLeft + maxAmountRight + currentNode.val > maxAmount)
{
maxAmount = maxAmountLeft + maxAmountRight + currentNode.val;
}
}

//Skip current
maxAmountLeft = 0;
maxAmountRight = 0;
MaxRob(currentNode.left, true, htMap, ref maxAmountLeft);
MaxRob(currentNode.right, true, htMap, ref maxAmountRight);

if (maxAmountLeft + maxAmountRight > maxAmount)
{
maxAmount = maxAmountLeft + maxAmountRight;
}

if (!htMap.ContainsKey(key))
{
htMap.Add(key, maxAmount);
}
}
}
}

Comments

  1. Cool problem! Since I'm a lazy potato I didn't bother using hashtable and used a RB-tree for a cache, but it's more than enough for good performance:

    using Cache = map, int>;
    class Solution {
    private:
    int rob(TreeNode* root, bool canRob, Cache& cache) {
    if (root == nullptr) return 0;
    pair key {root, canRob};
    auto cacheIt = cache.find(key);
    if (cacheIt != cache.end()) return cacheIt->second;
    int money = rob(root->left, true, cache) + rob(root->right, true, cache);
    if (canRob) money = max(money, root->val + rob(root->left, false, cache) + rob(root->right, false, cache));
    cache[key] = money;
    return money;
    }
    public:
    int rob(TreeNode* root) {
    Cache cache;
    return rob(root, true, cache);
    }
    };

    Small nit for your implementation - you don't need if (!htMap.ContainsKey(key)) in the end, since you've already checked this at the beginning of your method and that's the actual reason why you had to compete the maxAmount in the first place :)

    Reply Delete
  2. FYI: in C++ cache is actually not required to pass the tests ;) I used cache just to speed things up, even though technically the super simple solution below is good enough too:

    class Solution {
    private:
    int rob(TreeNode* root, bool canRob) {
    if (root == nullptr) return 0;
    int money = rob(root->left, true) + rob(root->right, true);
    if (canRob) money = max(money, root->val + rob(root->left, false) + rob(root->right, false));
    return money;
    }
    public:
    int rob(TreeNode* root) {
    return rob(root, true);
    }
    };

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...