当前位置:网站首页>leetcode 2097 — 合法重新排列数对
leetcode 2097 — 合法重新排列数对
2022-07-03 00:51:00 【coding-hwz】
一、题目描述

二、分析
不难发现,这个问题其实就是将不同的数字作为图中的顶点,将所有 pair 的头尾相连,然后找到一条访问所有边恰好一次的通路。
三、算法
1、欧拉通路
上述分析和欧拉通路的定义是相同的。欧拉图相关的定义和算法可以参考 『图论』入门以及 Hierholzer 算法。
该算法的描述如下:
(1)实现
下面的算法记录了所有点的入节点和出节点以比较:
struct Vertex {
unordered_set<int> ins;
unordered_set<int> outs;
};
vector<int> hierholzer(unordered_map<int, Vertex>& vertexes) {
// 确定欧拉回路的起点
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;
}
}
// 使用 dfs 确定环路的访问逆序
stack<int> s;
function<void(int)> dfs = [&](int index) {
while (1) {
if (vertexes[index].outs.empty()) {
break;
}
// 删除一条相邻边
auto iter = vertexes[index].outs.begin();
int next = *iter;
vertexes[index].outs.erase(iter);
vertexes[next].ins.erase(index);
dfs(next);
}
// 没有相邻边,将顶点入栈
if (vertexes[index].outs.empty()) {
s.push(index);
}
};
dfs(startIndex);
// 逆序返回
vector<int> ret;
while (!s.empty()) {
ret.push_back(s.top());
s.pop();
}
return ret;
}
(2)优化
实际上,除了确定起点时需要比较出度和入度,其余情况下我们只需要节点的出度。因此,我们可以仅保留入度的个数,而无需在过程中时刻维护入度的数量。在只需要更新出度节点的情况下,我们可以使用 vector 来优化删除速度。优化后的实现如下:
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、实现
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;
}
};

边栏推荐
- MongoDB系列之MongoDB常用命令
- Basic remote connection tool xshell
- 寻找标杆战友 | 百万级实时数据平台,终身免费使用
- Niu Ke swipes questions and clocks in
- MySQL basic usage 02
- 1038 Recover the Smallest Number
- Leetcode-934: the shortest Bridge
- excel表格计算时间日期的差值,并转化为分钟数
- Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
- Thank you for being together for these extraordinary two years!
猜你喜欢
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)

飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022

2022.2.14 resumption

Foundations of data science is free to download

excel去除小数点后面的数据,将数字取整

JS inheritance and prototype chain

Explain the basic concepts and five attributes of RDD in detail
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]

Linear programming of mathematical modeling (including Matlab code)
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
随机推荐
基本远程连接工具Xshell
Several cases of recursive processing organization
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
按键精灵打怪学习-前台和内网发送后台验证码
[AUTOSAR 11 communication related mechanism]
Initial order of pointer (basic)
MySQL basic usage 02
Thank you for being together for these extraordinary two years!
按键精灵打怪学习-自动寻路回打怪点
链表内指定区间反转
Telephone network problems
Every k nodes in the linked list are flipped
Button wizard play strange learning - go back to the city to buy medicine and add blood
研发一款国产ARM智能边缘计算网关需要什么
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
Basis of information entropy
MySQL multi table joint deletion
Understanding and distinguishing of some noun concepts in adjustment / filtering
Foundations of data science is free to download
Solve the cache problem of reactnative using WebView