摘要:復(fù)雜度思路對(duì)于每一個(gè)位置來(lái)說(shuō),考慮兩種情況分別對(duì)和再進(jìn)行計(jì)算。用對(duì)已經(jīng)計(jì)算過(guò)的進(jìn)行保留,避免重復(fù)計(jì)算。
LeetCode[337] House Robber III
recursion + MemorizationThe 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 1Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3 / 4 5 / 1 3 1Maximum amount of money the thief can rob = 4 + 5 = 9.
復(fù)雜度
O(N), O(lgN)
思路
對(duì)于每一個(gè)位置來(lái)說(shuō),考慮兩種情況, Max(child, subchild + root.val).
分別對(duì)child和subchild再進(jìn)行recursion計(jì)算。
用map對(duì)已經(jīng)計(jì)算過(guò)的node進(jìn)行保留,避免重復(fù)計(jì)算。
代碼
public int rob(TreeNode root) { // Base case; if(root == null) return 0; if(root.left == null && root.right == null) return root.val; if(map.containsKey(root)) return map.get(root); int child = 0, subchild = 0; if(root.left != null) { child += rob(root.left); subchild += rob(root.left.left) + rob(root.left.right); } if(root.right != null) { child += rob(root.right); subchild += rob(root.right.left) + rob(root.right.right); } int val = Math.max(child, subchild + root.val); map.put(root, val); return val; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65225.html
摘要:題目要求即如何從樹(shù)中選擇幾個(gè)節(jié)點(diǎn),在確保這幾個(gè)節(jié)點(diǎn)不直接相連的情況下使其值的和最大。當(dāng)前節(jié)點(diǎn)的情況有兩種選中或是沒(méi)選中,如果選中的話,那么兩個(gè)直接子節(jié)點(diǎn)將不可以被選中,如果沒(méi)選中,那么兩個(gè)直接子節(jié)點(diǎn)的狀態(tài)可以是選中或是沒(méi)選中。 題目要求 The thief has found himself a new place for his thievery again. There is on...
摘要:一番偵察之后,聰明的小偷意識(shí)到這個(gè)地方的所有房屋的排列類似于一棵二叉樹(shù)。如果兩個(gè)直接相連的房子在同一天晚上被打劫,房屋將自動(dòng)報(bào)警。計(jì)算在不觸動(dòng)警報(bào)的情況下,小偷一晚能夠盜取的最高金額。 Description The thief has found himself a new place for his thievery again. There is only one entranc...
摘要:解法真的非常巧妙,不過(guò)這道題里仍要注意兩個(gè)細(xì)節(jié)。中,為時(shí),返回長(zhǎng)度為的空數(shù)組建立結(jié)果數(shù)組時(shí),是包括根節(jié)點(diǎn)的情況,是不包含根節(jié)點(diǎn)的情況。而非按左右子樹(shù)來(lái)進(jìn)行劃分的。 Problem The thief has found himself a new place for his thievery again. There is only one entrance to this area,...
摘要:你不能連著偷兩家因?yàn)檫@樣會(huì)觸發(fā)警報(bào)系統(tǒng)。現(xiàn)在有一個(gè)數(shù)組存放著每一家中的可偷金額,問(wèn)可以偷的最大金額為多少這里考驗(yàn)了動(dòng)態(tài)編程的思想。動(dòng)態(tài)編程要求我們將問(wèn)題一般化,然后再找到初始情況開(kāi)始這個(gè)由一般到特殊的計(jì)算過(guò)程。 House Robber I You are a professional robber planning to rob houses along a street. Each...
摘要:注意對(duì)邊界條件的判斷,是否非空,是否長(zhǎng)度為 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...
閱讀 979·2021-11-22 09:34
閱讀 2163·2021-11-11 16:54
閱讀 2200·2021-09-27 14:00
閱讀 943·2019-08-30 15:55
閱讀 1531·2019-08-29 12:46
閱讀 603·2019-08-26 18:42
閱讀 642·2019-08-26 13:31
閱讀 3187·2019-08-26 11:52