算法day34-动态规划(7)

目录

  1. 打家劫舍
  2. 打家劫舍II
  3. 打家劫舍III

一、打家劫舍

 https://leetcode.cn/problems/house-robber/?envType=problem-list-v2&envId=8At1GmaZ

 

 

class Solution {
    public int rob(int[] nums) {
        if(nums.length == 0 || nums == null)    return 0;
        if(nums.length == 1)    return nums[0];
        int len = nums.length;
        int[] dp = new int[len];
        dp[0] = nums[0];
        dp[1] = Math.max(dp[0], nums[1]);       //偷第1家/不偷第0家
        for(int i=2; i<len; i++){
            dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i]);
        }
        return dp[len-1];
    }
}

 

二、打家劫舍II

 https://leetcode.cn/problems/house-robber-ii/description/?envType=problem-list-v2&envId=8At1GmaZ

 

class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];

        int len = nums.length;
        return Math.max(robAction(nums, 0, len - 2), robAction(nums, 1, len - 1));
    }

    public int robAction(int[] nums, int start, int end) {
        int n = end - start + 1;
        if (n == 0) return 0;
        if (n == 1) return nums[start];

        int[] dp = new int[n];
        dp[0] = nums[start];
        dp[1] = Math.max(nums[start], nums[start + 1]);

        for (int i = 2; i < n; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[start + i]);
        }
        return dp[n - 1];
    }
}

 

三、打家劫舍III

 https://leetcode.cn/problems/house-robber-iii/description/?envType=problem-list-v2&envId=8At1GmaZ

 

//方法1:记忆化搜索
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        Map<TreeNode, Integer> memo = new HashMap<>();
        return robAction(root, memo);
    }
    public int robAction(TreeNode root, Map<TreeNode, Integer> memo){
        if(root == null){
            return 0;
        }
        if(memo.containsKey(root)){
            return memo.get(root);
        }
        int money = root.val;
        if(root.left != null){
            money += robAction(root.left.left, memo) + robAction(root.left.right, memo);
        }
        if(root.right != null){
            money += robAction(root.right.left, memo) + robAction(root.right.right, memo);
        }
        int res = Math.max(money, robAction(root.left, memo)+robAction(root.right,memo));
        memo.put(root, res);
        return res;
    }
}
//方法2:动态规划
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        int[] res = robAction(root);
        return Math.max(res[0], res[1]);
    }
    public int[] robAction(TreeNode root){
        int[] res = new int[2];
        if(root == null){
            return res;
        }
        int[] left = robAction(root.left);
        int[] right = robAction(root.right);
        //不偷该节点,则左右孩子可以偷
        res[0] = Math.max(left[0], left[1]) + Math.max(right[0],right[1]);
        res[1] = root.val + left[0] + right[0];
        return res;
    }
}

 

posted @ 2025-06-05 10:08  筱倩  阅读(307)  评论(0)    收藏  举报