当前位置:网站首页>Leetcode 2097 - Legal rearrangement of pairs
Leetcode 2097 - Legal rearrangement of pairs
2022-07-03 01:15:00 【coding-hwz】
leetcode 2097 — Legal rearrangement of pairs
One 、 Title Description

Two 、 analysis
It's not hard to find out , This problem is actually taking different numbers as vertices in the graph , Will all pair End to end , Then find a path that accesses all edges exactly once .
3、 ... and 、 Algorithm
1、 Euler pathway
The above analysis is the same as the definition of Euler path . Relevant definitions and algorithms of Euler graph can be referred to 『 graph theory 』 Getting started and Hierholzer Algorithm .
The description of the algorithm is as follows :
(1) Realization
The following algorithm records the incoming and outgoing nodes of all points to compare :
struct Vertex {
unordered_set<int> ins;
unordered_set<int> outs;
};
vector<int> hierholzer(unordered_map<int, Vertex>& vertexes) {
// Determine the starting point of Euler circuit
int startIndex = vertexes.begin()->first;
int n = vertexes.size();
for (auto& vertex : vertexes) {
if (vertex.second.outs.size() - vertex.second.ins.size() == 1) {
startIndex = vertex.first;
}
}
// Use dfs Determine the access reverse order of the loop
stack<int> s;
function<void(int)> dfs = [&](int index) {
while (1) {
if (vertexes[index].outs.empty()) {
break;
}
// Delete an adjacent edge
auto iter = vertexes[index].outs.begin();
int next = *iter;
vertexes[index].outs.erase(iter);
vertexes[next].ins.erase(index);
dfs(next);
}
// No adjacent edges , Stack vertices
if (vertexes[index].outs.empty()) {
s.push(index);
}
};
dfs(startIndex);
// Reverse order return
vector<int> ret;
while (!s.empty()) {
ret.push_back(s.top());
s.pop();
}
return ret;
}
(2) Optimize
actually , In addition to determining the starting point, we need to compare the out degree and in degree , In other cases, we only need the output of nodes . therefore , We can only keep the number of degrees , There is no need to maintain the number of degrees at all times in the process . When you only need to update the out degree node , We can use vector To optimize the deletion speed . The optimized implementation is as follows :
vector<int> hierholzer(unordered_map<int, vector<int>>& edges, unordered_map<int, int>& inDegrees, unordered_map<int, int>& outDegrees) {
int startIndex = inDegrees.begin()->first;
for (auto& out : outDegrees) {
int index = out.first;
if (out.second > inDegrees[index]) {
startIndex = index;
break;
}
}
stack<int> s;
function<void(int)> dfs = [&](int index) {
while (!edges[index].empty()) {
auto next = edges[index].back();
edges[index].pop_back();
dfs(next);
}
s.push(index);
};
dfs(startIndex);
vector<int> ret;
while (!s.empty()) {
ret.push_back(s.top());
s.pop();
}
return ret;
}
2、 Realization
class Solution {
public:
vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
unordered_map<int, int> inDegrees, outDegrees;
unordered_map<int, vector<int>> edges;
for (auto& p : pairs) {
++outDegrees[p[0]];
++inDegrees[p[1]];
edges[p[0]].push_back(p[1]);
}
auto circle = hierholzer(edges, inDegrees, outDegrees);
int c = circle.size();
vector<vector<int>> ret(c - 1, vector<int>(2));
for (int i = 0; i < circle.size() - 1; ++i) {
ret[i][0] = circle[i];
ret[i][1] = circle[i + 1];
}
return ret;
}
};

边栏推荐
- 基本远程连接工具Xshell
- The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
- Trois tâches principales: asynchrone, courrier et timing
- Mongodb common commands of mongodb series
- The difference between relational database and non relational database
- [introduction to AUTOSAR seven tool chain]
- Rk3568 development board evaluation (II): development environment construction
- Inversion de l'intervalle spécifié dans la liste des liens
- On Fibonacci sequence
- [AUTOSAR eight OS]
猜你喜欢
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)

MySQL foundation 04 MySQL architecture

matlab 多普勒效应产生振动信号和处理

Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life

有向图的强连通分量

基本远程连接工具Xshell

信息熵的基础

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决

2022.2.14 resumption

Embrace the safety concept of platform delivery
随机推荐
Canvas drawing -- bingdd
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
MySQL basics 03 introduction to MySQL types
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
2022.2.14 resumption
Compare version number
Key wizard play strange learning - multithreaded background coordinate recognition
[introduction to AUTOSAR seven tool chain]
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
leetcode 2097 — 合法重新排列数对
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
(C language) data storage
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
Trois tâches principales: asynchrone, courrier et timing
基本远程连接工具Xshell
excel表格计算时间日期的差值,并转化为分钟数
[AUTOSAR XIII NVM]
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
电话网络问题
Draw love with go+ to express love to her beloved