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 hou…
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium 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…
198. House Robber 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 you from robbing each of them is that adjacent houses have security system connected…
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能获得的最大值 Paint House:用3种颜色,相邻的房屋不能用同一种颜色,求花费最小 Paint House II:用k种颜色,相邻的房屋不能用同一种颜色,求花费最小Paint Fence:用k种颜色,相邻的可以用同一种颜色,但不能超过连续的2个,求有多少种可能性 198. House Robb…
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 hou…
题目描述: 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 "a…
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 hou…
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 hou…
[抄题]: 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 "a…
每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{cur, false}]表示以cur为根的树,不偷cur节点的钱,最多能偷的钱 可以看出有下面的关系 mp[{node, false}] = mp[{node->left,true}] + mp[{node->right,true}] mp[{node, true}] = max(node->…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/house-robber-iii/description/ 题目描述 The thief has found himself a new place for his thievery again. There is only one entrance to this area…
二刷吧..不知道为什么house robbery系列我找不到笔记,不过印象中做了好几次了. 不是很难,用的post-order做bottom-up的运算. 对于一个Node来说,有2种情况,一种是选(自己+下下层):一种是选左右children. 其实就是选自己和不选自己的区别.其实更像是dfs的题而不是DP的题. Time: O(n) Space: O(lgn) public class Solution { public int rob(TreeNode root) { if (root =…
Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium 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…
小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警.在不触动警报的情况下,计算小偷一晚能盗取的最高金额.示例 1:     3    / \   2   3    \   \      3   1能盗取的最高金额 = 3 + 3 + 1 = 7.示例 2:     3    / \   4   5  / \…
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called 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…
198. House Robber 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 you from robbing each of them is that adjacent houses have security system connected…
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 hou…
题目描述 小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警. 在不触动警报的情况下,计算小偷一晚能盗取的最高金额. 示例 1: / \ 2 3 \ \ 3 1 能盗取的最高金额 = + + = 7. 示例 2: 3 / \ / \ \ 1 3 1 能盗取的最高金额 = + = 9. 解题思路 用后序遍历的…
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: //递归程序就是结构演绎,有点像dp,只要定义好每一次递归过程完成的是同一个目标,就能保证所有递归结束之后…
题目描述: 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为“根”. 除了“根”之外,每栋房子有且只有一个“父“房子与之相连.一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”. 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警. 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额. 示例 1: 输入: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1 输出: 7 解释: 小偷…
1. Description 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 tha…
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 hou…
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: 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 hous…
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数. n<=100000(这个数据范围是我凑出来的,但是我没试过更小的范围,BZOJ上没写数据范围(截止2017-08-24)) 题解 水题,开一个树状数组在线解决. 比如我们顺着扫过去,当到达一个 bj 时,我们求满足条件的 ai,bi 个数,其实就…
最近集中学习了一下矩阵树定理,自己其实还是没有太明白原理(证明)类的东西,但想在这里总结一下应用中的一些细节,矩阵树定理的一些引申等等. 首先,矩阵树定理用于求解一个图上的生成树个数.实现方式是:\(A\)为邻接矩阵,\(D\)为度数矩阵,则基尔霍夫(Kirchhoff)矩阵即为:\(K = D - A\).具体实现中,记 \(a\) 为Kirchhoff矩阵,则若存在 \(E(u, v)\) ,则\(a[u][u] ++, a[v][v] ++, a[u][v] --, a[v][u] --\…
题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输出 3 题解 树状数组 WH说是CDQ分治直接把我整蒙了... 把所有数按照第一次出现位置从小到大排序,然后扫一遍.此时一定是满足$a_j>a_i$的. 那么只需要求出$(a_j,b_j)$中以前出现过的$b_i$的数目.使用树状数组维护即可. 时间复杂度$O(n\log n)$ #include &l…
You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. Input The first line contains one integer n (1 <= n <=…
一.题目描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为“根”. 除了“根”之外,每栋房子有且只有一个“父“房子与之相连.一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”. 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警. 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额. 示例 1: 输入: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1 输出: 7 解释: 小…
题意:       给你一个初始序列,初始序列长度n,分别为1 2 3 4 5 ....n,有两种操作 (1)D l r 把l_r之间的数据都复制一遍 1 2 3 4 5 6 D 2 4 = 1 2 2 3 3 4 4 5 6 (2)Q l r 询问lr之间的数字出现的最大次数 1 2 2 3 3 4 4 4 5 Q 1 3 = 2 思路:       这个题目可以用线段树来解决,我们可以建一棵树1--n的,这个题目要注意一点就是无论怎么复制,所有的数字依然是连续的,对于线段树的每一个节点,我们…
T1 无聊的数列 来自:Link flag 帖先从水题入手. 首先分析题目,它是以等差数列为原型进行的修改.等差数列一大性质就是其差分数列的值除第一项以外均相等. 于是不难想到使用差分数列进行维护. 假设原数组为 \(A\),其差分数列为 \(num\).规定 \(num_i = A_i - A_{i - 1}(i \in [1, n])\). 当前更改区间 \(l, r\).需累加的等差数列首项为 \(k\),公差为 \(d\),长度为 \(r - l + 1\). 对于 \(l\),我们需要…