当前位置:网站首页>Leetcode daily question (968. binary tree cameras)
Leetcode daily question (968. binary tree cameras)
2022-07-03 09:33:00 【wangjun861205】
You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.
Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2:
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
Constraints:
- The number of nodes in the tree is in the range [1, 1000].
- Node.val == 0
Assign a number to each node in the tree , This is not a problem , left = curr * 2 + 1, right = curr * 2 + 2, hypothesis dp[i][is_covered][need_place] It represents the first in the tree i Whether nodes are covered by cameras (is_covered) And is it mandatory to install a camera on the node (need_place) In the case of i The minimum number of cameras required for the subtree of the root node . In this way, we can divide the situation into three kinds , One is the mandatory installation of cameras need_place == true, In this way, its left and right child nodes will be covered ( We are top-down , Regardless of parent node ), So the result is dp[i*2+1][true][false] + dp[i*2+2][true][false] + 1, If forced installation is not required , Let's see whether it covers (is_covered), If the current node has been overwritten , We can place cameras on the current node , That's the same as the situation mentioned above , dp[i*2+1][true][false] + dp[i*2+2][true][false] + 1, You can also not place , Because the current node has been overwritten , So even if we don't place cameras on the current node , There is no need to force the placement of cameras in that sub node , So the result is dp[i*2+1][false][false] + dp[i*2+2][false][false], The smaller of these two cases returns . The last is that there is neither mandatory placement nor coverage , Then we can place the camera on the current node ( ditto ), You can also force cameras to be placed on the left or right child nodes , dp[i*2+1][false][true] perhaps dp[i*2+2][false][true], Take the minimum value in these three cases
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
impl Solution {
fn dp(root: &Option<Rc<RefCell<TreeNode>>>, val: i32, is_covered: bool, need_place: bool, cache: &mut HashMap<(i32, bool, bool), i32>) -> i32 {
if let Some(node) = root {
if need_place {
let left = if let Some(c) = cache.get(&(val * 2 + 1, true, false)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, true, false, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, true, false)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, true, false, cache)
};
cache.insert((val, is_covered, need_place), left + right + 1);
return left + right + 1;
}
if is_covered {
let curr_place = {
let left = if let Some(c) = cache.get(&(val * 2 + 1, true, false)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, true, false, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, true, false)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, true, false, cache)
};
left + right + 1
};
let curr_pass = {
let left = if let Some(c) = cache.get(&(val * 2 + 1, false, false)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, false, false, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, false, false)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, false, false, cache)
};
left + right
};
let ans = curr_place.min(curr_pass);
cache.insert((val, is_covered, need_place), ans);
return ans;
}
let curr_place = {
let left = if let Some(c) = cache.get(&(val * 2 + 1, true, false)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, true, false, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, true, false)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, true, false, cache)
};
left + right + 1
};
let left_place = {
if node.borrow().left.is_none() {
i32::MAX
} else {
let left = if let Some(c) = cache.get(&(val * 2 + 1, false, true)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, false, true, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, false, false)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, false, false, cache)
};
left + right
}
};
let right_place = {
if node.borrow().right.is_none() {
i32::MAX
} else {
let left = if let Some(c) = cache.get(&(val * 2 + 1, false, false)) {
*c
} else {
Solution::dp(&node.borrow().left, val * 2 + 1, false, false, cache)
};
let right = if let Some(c) = cache.get(&(val * 2 + 2, false, true)) {
*c
} else {
Solution::dp(&node.borrow().right, val * 2 + 2, false, true, cache)
};
left + right
}
};
let ans = curr_place.min(left_place).min(right_place);
cache.insert((val, is_covered, need_place), ans);
return ans;
}
0
}
pub fn min_camera_cover(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut cache = HashMap::new();
Solution::dp(&root, 0, false, false, &mut cache)
}
}
边栏推荐
- npm install安装依赖包报错解决方法
- Flink学习笔记(十)Flink容错机制
- Apply for domain name binding IP to open port 80 record
- Beego learning - JWT realizes user login and registration
- Please tell me how to set vscode
- 解决Editor.md上传图片获取不到图片地址问题
- Call the contents of Excel cells opened at the same time - button line feed
- Detailed steps of windows installation redis
- [kotlin learning] classes, objects and interfaces - define class inheritance structure
- CATIA automation object architecture - detailed explanation of application objects (III) systemservice
猜你喜欢
Build a solo blog from scratch
Navicat, MySQL export Er graph, er graph
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Spark structured stream writing Hudi practice
Hudi data management and storage overview
2022-2-14 learning the imitation Niuke project - send email
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
Go language - Reflection
Vscode编辑器右键没有Open In Default Browser选项
随机推荐
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Explanation of the answers to the three questions
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
Spark structured stream writing Hudi practice
专利查询网站
Detailed steps of windows installation redis
The server denied password root remote connection access
Jenkins learning (II) -- setting up Chinese
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
解决Editor.md上传图片获取不到图片地址问题
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
Computing level network notes
Call the contents of Excel cells opened at the same time - button line feed
ERROR: certificate common name “*.” doesn’t match requested ho
Jetson Nano 自定义启动图标kernel Logo cboot logo
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
Spark overview
307. Range Sum Query - Mutable
LeetCode每日一题(2109. Adding Spaces to a String)
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)