当前位置:网站首页>leetcode 310. Minimum Height Trees
leetcode 310. Minimum Height Trees
2022-07-04 06:29:00 【sure00】
The title is to give a picture , Please lift a node , The height of the formed tree is the smallest .
The first idea is violent search .
Raise every point , Then calculate and compare the height of the tree .
The result is timeout .
Optimized some , It is found that there is still no way to deal with it within the specified time .
I saw the great God on the Internet huifeng guan And the solution before the cruel problem group , I feel too low 了 .
Since the topic requires that the height of the tree after lifting a node is the smallest , Under the problem transformation, the distance from a certain node to each other node is the shortest .
The layout of my high school building is similar to that of this problem , The building of grade one in North America , high , Senior 3 laboratory building , Teacher building , music , Gymnasium . They are all linked by stairs .
Our problem is like arranging the office of the high school teaching director , To find a suitable position . This can quickly reach the battlefield .
Then the problem is simpler , Arrange the office of the director of education to the most central building in all buildings .
The first example shows , Arrange the teaching director in 1 You can reach the battlefield at the first time .
For the second example
If you want to find the core position, you have to peel onions , Peel layer by layer .
First peel off the outermost layer of onion ( floor ), That is to say 5, 0, 1, 2 These outermost onions ( floor ), Then it's left 3,4.
There is no way to peel these two buildings , Even the existence of the teaching director can be eliminated .
In this case , Arrange the teaching director to 3 perhaps 4 Building No .
Use topological sorting to peel from the outside , Peel to the last layer . What remains is the answer .
class Solution {
public:
unordered_map<int ,vector<int>> buildGraphic(vector<vector<int>>& edges)
{
unordered_map<int ,vector<int>> neighbors;
for(auto edge: edges)
{
neighbors[edge[0]].push_back(edge[1]);
neighbors[edge[1]].push_back(edge[0]);
}
return neighbors;
}
unordered_map<int, int> buildDegree(unordered_map<int ,vector<int>> neighbors){
unordered_map<int, int> degree;
for(auto neighbor: neighbors){
degree[neighbor.first] = neighbor.second.size();
}
return degree;
}
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
//build graphic
//set every node as root node and using bfs go through the tree level by level.
//push the root if the levels is min.
vector<int> ret;
if(edges.size() ==0 && n !=0)
for(int i=0;i<n;i++)
ret.push_back(i);
unordered_map<int ,vector<int>> neighbors = buildGraphic(edges);
unordered_map<int, int> degrees = buildDegree(neighbors);
unordered_set<int> accessed;
queue<int> myqueue;
//push the most out building into the queue
for(auto degree: degrees) {
if(degree.second == 1)
myqueue.push(degree.first);
}
while(!myqueue.empty()) {
ret.clear();
int size = myqueue.size();
for(int i=0;i<size;i++) {
int cur=myqueue.front();
myqueue.pop();
ret.push_back(cur);
for(auto neighbor: neighbors[cur]){
degrees[neighbor]--;
if(degrees[neighbor] == 1)
myqueue.push(neighbor);
}
}
}
return ret;
}
};
边栏推荐
- 微信小程序使用rich-text中图片宽度超出问题
- Functions in C language (detailed explanation)
- ABAP:OOALV实现增删改查功能
- Design and implementation of redis 7.0 multi part AOF
- Considerations for testing a website
- gslb(global server load balance)技术的一点理解
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
- Variables d'environnement personnalisées uniapp
- 《ClickHouse原理解析与应用实践》读书笔记(4)
- 24 magicaccessorimpl can access the debugging of all methods
猜你喜欢
随机推荐
Json Web token - jwt vs. Traditional session login Authentication
High performance parallel programming and optimization | lesson 02 homework at home
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
SQL injection SQL lab 11~22
Design and implementation of redis 7.0 multi part AOF
[untitled]
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
Uninstall Google drive hard drive - you must exit the program to uninstall
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
采用中微BATG135实现IIC数据/指令交互
QT get random color value and set label background color code
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Bicolor case
How to determine whether an array contains an element
27-31. Dependency transitivity, principle
Gridview出现滚动条,组件冲突,如何解决
我的NVIDIA开发者之旅——优化显卡性能