国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] House Robber III

macg0406 / 1166人閱讀

摘要:解法真的非常巧妙,不過這道題里仍要注意兩個細(xì)節(jié)。中,為時,返回長度為的空數(shù)組建立結(jié)果數(shù)組時,是包括根節(jié)點的情況,是不包含根節(jié)點的情況。而非按左右子樹來進(jìn)行劃分的。

Problem

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
  3
 / 
2   3
     
  3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

    3
   / 
  4   5
 /     
1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

Note

DFS解法真的非常巧妙,不過這道題里仍要注意兩個細(xì)節(jié)。
DFS中,root為null時,返回長度為2的空數(shù)組;
建立結(jié)果數(shù)組A時,A[0]是包括根節(jié)點的情況,A[1]是不包含根節(jié)點的情況。而非按左右子樹來進(jìn)行劃分的。

Solution
public class Solution {
    public int houseRobber3(TreeNode root) {
        int[] A = dfs(root);
        return Math.max(A[0], A[1]);
    }
    public int[] dfs(TreeNode root) {
        if (root == null) return new int[2];
        int[] left = dfs(root.left);
        int[] right = dfs(root.right);
        int[] A = new int[2];
        A[0] = left[1] + root.val + right[1];
        A[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        return A;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/65853.html

相關(guān)文章

  • [LintCode/LeetCode] House Robber II

    摘要:因為取了隊首就不能取隊尾,所以對數(shù)組循環(huán)兩次,一個從取到,一個從取到,比較兩次循環(huán)后隊尾元素,取較大者。注意,要先討論當(dāng)原數(shù)組位數(shù)小于時的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...

    OnlyLing 評論0 收藏0
  • LeetCode[337] House Robber III

    摘要:復(fù)雜度思路對于每一個位置來說,考慮兩種情況分別對和再進(jìn)行計算。用對已經(jīng)計算過的進(jìn)行保留,避免重復(fù)計算。 LeetCode[337] House Robber III The thief has found himself a new place for his thievery again. There is only one entrance to this area, calle...

    Dr_Noooo 評論0 收藏0
  • leetcode337. House Robber III

    摘要:題目要求即如何從樹中選擇幾個節(jié)點,在確保這幾個節(jié)點不直接相連的情況下使其值的和最大。當(dāng)前節(jié)點的情況有兩種選中或是沒選中,如果選中的話,那么兩個直接子節(jié)點將不可以被選中,如果沒選中,那么兩個直接子節(jié)點的狀態(tài)可以是選中或是沒選中。 題目要求 The thief has found himself a new place for his thievery again. There is on...

    AlphaWallet 評論0 收藏0
  • LeetCode 337. House Robber III

    摘要:一番偵察之后,聰明的小偷意識到這個地方的所有房屋的排列類似于一棵二叉樹。如果兩個直接相連的房子在同一天晚上被打劫,房屋將自動報警。計算在不觸動警報的情況下,小偷一晚能夠盜取的最高金額。 Description The thief has found himself a new place for his thievery again. There is only one entranc...

    ymyang 評論0 收藏0
  • [LintCode/LeetCode] Paint House I & Paint Hous

    摘要:在原數(shù)組上動規(guī),每一行對應(yīng)一個房子,每一個元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個元素該元素的值上一行兩個與該元素不同列元素的值的較小者。不過這次要記錄三個變量本行最小值,本行第二小值,本行最小值下標(biāo)。 Paint House Problem There are a row of n houses, each house can be painted ...

    ChristmasBoy 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<