打家劫舍

題目一

你是一個專業的小偷,計劃偷竊沿街的房屋。每間房內都藏有一定的現金,影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警

給定一個代表每個房屋存放金額的非負整數數組,計算你在不觸動警報裝置的情況下,能夠偷竊到的最高金額。

示例 1:
輸入: [1,2,3,1]
輸出: 4
解釋: 偷竊 1 號房屋 (金額 = 1) ,然后偷竊 3 號房屋 (金額 = 3)。
偷竊到的最高金額 = 1 + 3 = 4 。

示例 2:
輸入: [2,7,9,3,1]
輸出: 12
解釋: 偷竊 1 號房屋 (金額 = 2), 偷竊 3 號房屋 (金額 = 9),接著偷竊 5 號房屋 (金額 = 1)。
偷竊到的最高金額 = 2 + 9 + 1 = 12 。

方法:動態規劃
dp[i]表示i個房子時能搶到最多的前
dp[0]=nums[0]
dp[1]=max(nums[0],nums[1])
dp[i]=max(dp[i-1],dp[i-2]+nums[i])
dp[n]即為最終結果

public int rob(int[] nums) {
    if (nums == null || nums.length == 0) {
        return 0;
    }
    if (nums.length == 1) {
        return nums[0];
    }
    int[] dp = new int[nums.length];
    dp[0] = nums[0];
    dp[1] = Math.max(nums[0], nums[1]);
    for (int i = 2; i < nums.length; i++) {
        dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
    }
    return dp[nums.length - 1];
}

優化空間復雜度

public int rob(int[] nums) {
    int preMax = 0;//前2個位置
    int curMax = 0;//前1個位置
    for (int i = 0; i < nums.length; i++) {
        int temp = curMax;
        curMax = Math.max(curMax, preMax + nums[i]);
        preMax = temp;
    }
    return curMax;
}

題目二

當房子為一個環時
思路:第一個房子和最后一個房子只能搶一個,兩次遍歷就可以了

public int rob(int[] nums) {
    if(nums.length==1){
        return nums[0];
    }
    int robFirstMax=robHelper(nums,0,nums.length-2);
    int robLastMax=robHelper(nums,1,nums.length-1);
    return Math.max(robFirstMax,robLastMax);
}

public int robHelper(int[] nums,int s,int t){
    int preMax = 0;//前2個位置
    int curMax = 0;//前1個位置
    for (int i = s; i <=t; i++) {
        int temp = curMax;
        curMax = Math.max(curMax, preMax + nums[i]);
        preMax = temp;
    }
    return curMax;
}

題目3

為一棵二叉樹

示例 1:

輸入: [3,2,3,null,3,null,1]

    3
    / \
   2   3
    \   \ 
     3   1

輸出: 7 
解釋: 小偷一晚能夠盜取的最高金額 = 3 + 3 + 1 = 7

示例 2:

輸入: [3,4,5,1,3,null,1]

     3
    / \
   4  5
  / \   \ 
 1   3   1

輸出: 9
解釋: 小偷一晚能夠盜取的最高金額 = 4 + 5 = 9

方法一:暴力遞歸
當前結點能搶到的錢為兩個兒子能搶到的錢VS自己+四個孫子能搶到的錢

public int rob(TreeNode root) {
    if(root==null){
        return 0;
    }
    int sonRob=rob(root.left)+rob(root.right);
    int grandSobRob=root.val;
    if(root.left!=null){
        grandSobRob+=rob(root.left.left)+rob(root.left.right);
    }
    if(root.right!=null){
        grandSobRob+=rob(root.right.left)+rob(root.right.right);
    }
    return Math.max(sonRob,grandSobRob);
}

方法二:備忘錄法

Map<TreeNode,Integer> map=new HashMap<>();

public int rob(TreeNode root) {
    if(root==null){
        return 0;
    }
    Integer money=map.get(root);
    if(money!=null){
        return money;
    }
    int sonRob=rob(root.left)+rob(root.right);
    int grandSobRob=root.val;
    if(root.left!=null){
        grandSobRob+=rob(root.left.left)+rob(root.left.right);
    }
    if(root.right!=null){
        grandSobRob+=rob(root.right.left)+rob(root.right.right);
    }
    int maxMoney=Math.max(sonRob,grandSobRob);
    map.put(root,maxMoney);
    return maxMoney;
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。