当前位置:网站首页>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)
}
}
边栏推荐
- Win10 quick screenshot
- 2022-2-13 learning the imitation Niuke project - home page of the development community
- Word segmentation in full-text indexing
- [set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
- ERROR: certificate common name “*.” doesn’t match requested ho
- Serializer rewrite: update and create methods
- IDEA 中使用 Hudi
- Equality judgment of long type
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- Vscode Arduino installation Library
猜你喜欢

Flink学习笔记(十一)Table API 和 SQL

Flink-CDC实践(含实操步骤与截图)

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

There is no open in default browser option in the right click of the vscade editor

软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数

About the configuration of vs2008+rade CATIA v5r22
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords

LeetCode每日一题(931. Minimum Falling Path Sum)

Hudi learning notes (III) analysis of core concepts
随机推荐
Solve POM in idea Comment top line problem in XML file
一款开源的Markdown转富文本编辑器的实现原理剖析
DSP data calculation error
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
Redis learning (I)
Powerdesign reverse wizard such as SQL and generates name and comment
Temper cattle ranking problem
Jenkins learning (I) -- Jenkins installation
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
Esp32 at command does not respond
Hudi 数据管理和存储概述
文件系统中的目录与切换操作
Flink-CDC实践(含实操步骤与截图)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
Spark 集群安装与部署
Hudi quick experience (including detailed operation steps and screenshots)
Run flash demo on ECS
Word segmentation in full-text indexing
Usage of pandas to obtain MySQL data
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis