Sunday, January 27, 2019

337. House Robber III

三刷 07/2022
Version #2 Bottom up DFS
Time O(N)
Space O(H) - height of the tree, worst O(N)
Runtime: 1 ms, faster than 92.27% of Java online submissions for House Robber III.
Memory Usage: 44.8 MB, less than 29.44% of Java online submissions for House Robber III.
class Solution {
    public int rob(TreeNode root) {
        int[] result = robHelper(root);
        return Math.max(result[0], result[1]);
    }
    
    private int[] robHelper(TreeNode node) {
        int[] result = new int[2];
        if (node == null) {
            return result;
        }
        int[] left = robHelper(node.left);
        int[] right = robHelper(node.right);
        result[0] = node.val + left[1] + right[1];
        result[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        return result;
    }
}





二刷  06/2022
Version #2 Bottom up DFS
我们return一个int[]表示rob current node/ not rob current node两个结果
每个node保证只visited一次
Time O(N)
Space - depth of the stack, O(N) worst, O(logN) best

Runtime: 0 ms, faster than 100.00% of Java online submissions for House Robber III.
Memory Usage: 41.5 MB, less than 98.09% of Java online submissions for House Robber III.

class Solution {
    public int rob(TreeNode root) {
        // For each node, return two values - int[0] max if rob the house, int[1] max of the children no matter rob or not rob
        int[] res = robHelper(root);
        return Math.max(res[0], res[1]);
    }
    
    private int[] robHelper(TreeNode node) {
        if (node == null) {
            return new int[]{0, 0};
        }
        // leaf
        if (node.left == null && node.right == null) {
            return new int[]{node.val, 0};
        }
        int[] left = robHelper(node.left);
        int[] right = robHelper(node.right);
        // if we rob the house, then we cannot rob it's children
        int curr = node.val + left[1] + right[1];
        // if not rob, we are free to choose rob the children or not
        int children = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        return new int[]{curr, children};
    }


一刷
Version #1 DFS


67.32 %
class Solution {
    public int rob(TreeNode root) {
        Map<TreeNode, Integer> map = new HashMap<>();
        return dfs(root, map);
    }
   
    private int dfs(TreeNode node, Map<TreeNode, Integer> map) {
        // max amount of money we can get if we enter through this node
        if (node == null) {
            return 0;
        }
        if (map.containsKey(node)) return map.get(node);
       
        // if we rob this node, then we can't rob any of its child node
        int curr = node.val;
        if (node.left != null) {
            curr += dfs(node.left.left, map) + dfs(node.left.right, map);
        }
        if (node.right != null) {
            curr += dfs(node.right.left, map) + dfs(node.right.right, map);
        }
       
        curr = Math.max(curr, dfs(node.left, map) + dfs(node.right, map));
        map.put(node, curr);
        return curr;
    }
}

No comments:

Post a Comment