当前位置:网站首页>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;
}
};

边栏推荐
- 产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
- 2022 coal mine gas drainage examination question bank and coal mine gas drainage examination questions and analysis
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- matlab查找某一行或者某一列在矩阵中的位置
- MySQL foundation 06 DDL
- [untitled]
- 数学建模之线性规划(含MATLAB代码)
- Linear programming of mathematical modeling (including Matlab code)
- Compare version number
- MySQL foundation 07-dcl
猜你喜欢

dotConnect for PostgreSQL数据提供程序

MySQL basic usage 02

JS inheritance and prototype chain

详解RDD基本概念、RDD五大属性

leetcode 6103 — 从树中删除边的最小分数

【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
![[AUTOSAR eight OS]](/img/ac/fbc84c077ff9c94c840e1871171d19.png)
[AUTOSAR eight OS]

Assets, vulnerabilities, threats and events of the four elements of safe operation

Trois tâches principales: asynchrone, courrier et timing

The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
随机推荐
Key wizard play strange learning - front desk and Intranet send background verification code
KingbaseES ALTER TABLE 中 USING 子句的用法
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Button wizard play strange learning - go back to the city to buy medicine and add blood
Merge K sorted linked lists
有向图的强连通分量
How to convert Quanzhi a40i/t3 to can through SPI
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
按键精灵打怪学习-多线程后台坐标识别
MySQL
Basic use of sringcloud & use of component Nacos
Solve the cache problem of reactnative using WebView
MySQL basics 03 introduction to MySQL types
Canvas drawing -- bingdd
[introduction to AUTOSAR seven tool chain]
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Strongly connected components of digraph
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
[shutter] animation animation (shutter animation type | the core class of shutter animation)
强化学习 Q-learning 实例详解