首页 > 其他分享 >337. 打家劫舍 III

337. 打家劫舍 III

时间:2023-05-31 21:22:37浏览次数:35  
标签:right return cur III 337 打家劫舍 root rob left

小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为 root 。

除了 root 之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果 两个直接相连的房子在同一天晚上被打劫 ,房屋将自动报警。

给定二叉树的 root 。返回 在不触动警报的情况下 ,小偷能够盗取的最高金额 。


输入: root = [3,2,3,null,3,null,1]
输出: 7 
解释: 小偷一晚能够盗取的最高金额 3 + 3 + 1 = 7

> 递归(优化)


class Solution {
public:
    unordered_map<TreeNode* , int> umap; // 记录计算过的结果
    int rob(TreeNode* root) {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right == NULL) return root->val;
        if (umap[root]) return umap[root]; // 如果umap里已经有记录则直接返回
        // 偷父节点
        int val1 = root->val;
        if (root->left) val1 += rob(root->left->left) + rob(root->left->right); // 跳过root->left
        if (root->right) val1 += rob(root->right->left) + rob(root->right->right); // 跳过root->right
        // 不偷父节点
        int val2 = rob(root->left) + rob(root->right); // 考虑root的左右孩子
        umap[root] = max(val1, val2); // umap记录一下结果
        return max(val1, val2);
    }
};

> 动态规划


class Solution {
public:
    int rob(TreeNode* root) {
        vector<int> result = robTree(root);
        return max(result[0], result[1]);
    }
    // 长度为2的数组,0:不偷,1:偷
    vector<int> robTree(TreeNode* cur) {
        if (cur == NULL) return vector<int>{0, 0};
        vector<int> left = robTree(cur->left);
        vector<int> right = robTree(cur->right);
        // 偷cur,那么就不能偷左右节点。
        int val1 = cur->val + left[0] + right[0];
        // 不偷cur,那么可以偷也可以不偷左右节点,则取较大的情况
        int val2 = max(left[0], left[1]) + max(right[0], right[1]);
        return {val2, val1};
    }
};

标签:right,return,cur,III,337,打家劫舍,root,rob,left
From: https://www.cnblogs.com/lihaoxiang/p/17447355.html

相关文章

  • 213. 打家劫舍 II
    你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的......
  • leetcode 557. Reverse Words in a String III
    Givenastring,youneedtoreversetheorderofcharactersineachwordwithinasentencewhilestillpreservingwhitespaceandinitialwordorder.Example1:Input:"Let'stakeLeetCodecontest"Output:"s'teLekatedoCteeLtsetno......
  • 链表应用 III
    目录链表应用应用1:Leetocde.21题目分析代码实现方法一:迭代实现方法一:递归实现应用2:Leetocde.23题目分析方法一:分治方法二:优先级队列代码实现方法一:分治方法二:优先级队列应用3:Leetocde.141题目分析代码实现应用4:Leetocde.142题目分析代码实现应用5:Leetocde.876题目分析代码实现应用......
  • III.追想 题解
    原题链接我第一次出的一道比较正经的菜题,欢迎大家来切哦。感谢魔法少女老干妈GM_Joanna_的支持对于操作1,3:注意到1e9的数据至多5此操作就能把一个位置变为0,这个次数可视为常数。考虑每个位置暴力改,也只会递归\(5\timesn\logn\)次。对于3操作,考虑最坏的情况,每......
  • Nas Docker 安装个人记账web项目:firefly_iii &beancount-gs
    NasDocker安装个人记账web项目:firefly_iii&beancount-gs1.经过搜索以及GPT的询问,通过预览界面感觉firefly_iii官方示例demo:https://demo.firefly-iii.org/官方安装文档:https://docs.firefly-iii.org/firefly-iii/installation/docker/本人采用的是群晖Nasdocker安装:这个......
  • 123. 买卖股票的最佳时机 III
    classSolution{public:intmaxProfit(vector<int>&prices){intn=prices.size();vector<int>f(n+1),g(n+1);//预处理f,f[i]表示前i天交易的最大值for(inti=1,min_price=INT_MAX;i<=n;i++)//根据当前是否卖出划分{......
  • leetcode 1084 销售分析III
    leetcode1084销售分析IIIselectdistinctp.product_id,p.product_namefromProductpleftjoinSalessonp.product_id=s.product_idwheres.product_idnotin(selectproduct_idfromSaleswheresale_date<'2019-01-01'orsale_d......
  • LeetCode198. 打家劫舍
    classSolution{public:intf[110],g[110];//分别表示第i个房屋偷,不偷的最大价值introb(vector<int>&nums){intn=nums.size();for(inti=1;i<=n;i++){g[i]=max(f[i-1],g[i-1]);f[i]=g[i-1]+nums[i-1];......
  • NC53370 Forsaken的三维数点
    题目链接题目题目描述​Forsaken现在在一个三维空间中,空间中每个点都可以用\((x,y,z)\)表示。突然,三维空间的主人出现了,如果Forsaken想要继续在三维空间中呆下去,他就必须回答三维空间主人的问题。​主人会在空间中坐标为\((x,y,z)\)处加一点能量值,当他加了一......
  • Intel Pentium III 512MB内存 i815集显上安装Ubuntu Server 14.04
    自己的御用奔腾IIIPC,接口齐全,准备安装UbuntuServer14.04i386,继续发挥余热,物尽其用。 基本配置:CPU:IntelPentiumIII1000MHz,256KBL2,133MHzFSB,0.18um,1.75v,Coppermine-TRAM:512MBSDRAM,PC133GPU:Inteli82815IGPHDD:128GBSSD, withSATAtoIDEa......