当前位置:网站首页>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)
}
}
边栏推荐
- ERROR: certificate common name “*.” doesn’t match requested ho
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
- Win10安装ELK
- C language programming specification
- Win10 quick screenshot
- WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
- The idea of compiling VBA Encyclopedia
- Long类型的相等判断
- Utilisation de hudi dans idea
猜你喜欢
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Using Hudi in idea
Detailed steps of windows installation redis
Win10 install elk
Flink学习笔记(八)多流转换
Crawler career from scratch (IV): climb the bullet curtain of station B through API
IDEA 中使用 Hudi
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
Alibaba cloud notes for the first time
随机推荐
Jetson Nano 自定义启动图标kernel Logo cboot logo
Go language - Reflection
Flink学习笔记(十一)Table API 和 SQL
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
LeetCode每日一题(516. Longest Palindromic Subsequence)
The server denied password root remote connection access
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
Beego learning - JWT realizes user login and registration
基于opencv实现桌面图标识别
专利查询网站
Redis learning (I)
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
ERROR: certificate common name “*.” doesn’t match requested ho
Run flash demo on ECS
Hudi learning notes (III) analysis of core concepts
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)