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

边栏推荐
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- 详解RDD基本概念、RDD五大属性
- Key wizard play strange learning - front desk and Intranet send background verification code
- What is needed to develop a domestic arm intelligent edge computing gateway
- 瑞萨电子RZ/G2L开发板上手评测
- Trois tâches principales: asynchrone, courrier et timing
- [AUTOSAR nine c/s principle Architecture]
- 鏈錶內指定區間反轉
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
猜你喜欢

Merge K sorted linked lists

The difference between tail -f, tail -f and tail
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)

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

全志A40i/T3如何通过SPI转CAN

这不平凡的两年,感谢我们一直在一起!

2022.2.14 resumption

(C language) data storage
![[AUTOSAR twelve mode management]](/img/42/292e3da3f5d488a1e8c10ea9bbfbab.png)
[AUTOSAR twelve mode management]

每日一题之干草堆的移动
随机推荐
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
KingbaseES ALTER TABLE 中 USING 子句的用法
[C language] branch and loop statements (Part 1)
按鍵精靈打怪學習-多線程後臺坐標識別
MySQL基础用法02
Leetcode-2280: represents the minimum number of line segments of a line graph
Esp32 simple speed message test of ros2 (limit frequency)
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
详解RDD基本概念、RDD五大属性
[shutter] animation animation (shutter animation type | the core class of shutter animation)
MySQL basics 03 introduction to MySQL types
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Excel calculates the difference between time and date and converts it into minutes
1038 Recover the Smallest Number
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
关于Fibonacci数列
Rk3568 development board evaluation (II): development environment construction
(C语言)数据的存储
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】