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

边栏推荐
- 这不平凡的两年,感谢我们一直在一起!
- 合并K个已排序的链表
- Key wizard play strange learning - front desk and Intranet send background verification code
- 递归处理组织的几种情况
- Asynchronous, email and scheduled tasks
- 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
- Basic concept and implementation of overcoming hash
- KingbaseES ALTER TABLE 中 USING 子句的用法
- 每日一题之干草堆的移动
- 465. 最优账单平衡 DFS 回溯
猜你喜欢
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

瑞萨RZ/G2L ARM开发板存储读写速度与网络实测

Correctly distinguish the similarities and differences among API, rest API, restful API and web service

12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet

Merge K sorted linked lists
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

How to convert Quanzhi a40i/t3 to can through SPI

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities

基本远程连接工具Xshell
随机推荐
Thank you for being together for these extraordinary two years!
The difference between tail -f, tail -f and tail
[AUTOSAR nine c/s principle Architecture]
[overview of AUTOSAR four BSW]
寻找标杆战友 | 百万级实时数据平台,终身免费使用
Linear programming of mathematical modeling (including Matlab code)
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Key wizard play strange learning - front desk and Intranet send background verification code
Initial order of pointer (basic)
(C language) data storage
拥抱平台化交付的安全理念
【无标题】
[AUTOSAR twelve mode management]
按鍵精靈打怪學習-多線程後臺坐標識別
MySQL基础用法02
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
Mongodb common commands of mongodb series
Daily topic: movement of haystack
[shutter] image component (cached_network_image network image caching plug-in)