当前位置:网站首页>Leetcode sword finger offer II 115. reconstruction sequence: diagram topology sorting
Leetcode sword finger offer II 115. reconstruction sequence: diagram topology sorting
2022-07-23 18:41:00 【Tisfy】
【LetMeFly】 The illustration : The finger of the sword Offer II 115. Reconstruction sequence - A topological sort
Force button topic link :https://leetcode.cn/problems/ur2n8P/
Please judge the original sequence org Whether it can be from the sequence set seqs Only in The reconstruction .
Sequence org yes 1 To n Arrangement of integers , among 1 ≤ n ≤ 104. The reconstruction Refers to the sequence set seqs Build the shortest public supersequence in , namely seqs Any sequence in is a subsequence of the shortest sequence .
Example 1:
Input : org = [1,2,3], seqs = [[1,2],[1,3]] Output : false explain :[1,2,3] Not the only sequence that can be reconstructed , because [1,3,2] It's also a legal sequence .
Example 2:
Input : org = [1,2,3], seqs = [[1,2]] Output : false explain : The only sequence that can be reconstructed is [1,2].
Example 3:
Input : org = [1,2,3], seqs = [[1,2],[1,3],[2,3]] Output : true explain : Sequence [1,2], [1,3] and [2,3] Can be uniquely reconstructed into the original sequence [1,2,3].
Example 4:
Input : org = [4,1,5,2,6,3], seqs = [[5,2,6,3],[4,1,5,2]] Output : true
Tips :
1 <= n <= 104orgIt's the number.1TonAn arrangement of1 <= segs[i].length <= 105seqs[i][j]yes32Bit signed integer
Be careful : This topic and the main station 444 The question is the same :https://leetcode-cn.com/problems/sequence-reconstruction/
Method 1 : A topological sort
We analyze according to the example :
Example 1 :
nums = [1,2,3], sequences = [[1,2],[1,3]]
In example 1 , We know the permutation [1, 2, 3] Two subsequences of [1, 2] and [1, 3]. This means that :1 Must appear in 2 And 1 Must appear in 3 In front of .( Because the relative position of the elements in the subsequence must remain unchanged )
however 2 and 3 Which is in the front and which is in the back ? According to the given input [[1,2],[1,3]] Unable to judge .
therefore , Example 1 cannot be uniquely determined “ Supersequence ”
Example 2 :
nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
In example 2 , We know the permutation [1, 2, 3] Three subsequences of [1, 2]、[1, 3] and [2, 3]. This means that 1 stay 2 front 、1 stay 3 front 、2 stay 3 front .
Then the above three conditions should be met , There is only one arrangement :[1, 2, 3]
Therefore, example 2 can be uniquely determined “ Supersequence ”[1, 2, 3]
Realize the idea :
“1 stay 2 front 、1 stay 3 front ” It's easy for us to think A topological sort .
We can build a graph , The nodes in the figure are nums Every element in . If 1 stay 2 Add one before 1→2 The edge of .
Then the diagram of example 1 will be constructed as :
![[[1, 2], [1, 3]].png](/img/27/fb7b741d5ea7f253d02040fb25071e.png)
from The degree of 0 The node of 1 Start topological sorting , After sorting, we found that there are two nodes left , There is no relative order between them .
The diagram of example 2 will be constructed as :
![[[1, 2], [1, 3], [2, 3]].png](/img/55/1f3db3c98932bb61498107d6750da7.png)
from The degree of 0 The node of 1 Start topological sorting , After sorting, only the final node is left 3
Specific implementation method
- Initially traverse
mermaid sequenceDiagramsAll elements in , aboutmermaid sequenceDiagramsMedium[a, b, c], Build aa→bEdge and ab→cThe edge of , And putbandcThe degree of+1 - Traverse all nodes , The degree of engagement is 0 Join the team . Constantly take nodes out of the queue , Remove all edges starting from this node , And the depth of the node pointed by the removed edge -1.( Suppose the slave node
aThere are two sides to starta→banda→c, thatbandcThe degree of -1) - Until the queue is empty
Be careful :
- During the whole sorting process , At most in the queue 1 Nodes .( That's because if there are multiple degrees at the same time 0 The node of , It is impossible to judge the relative order between these nodes )
- After sorting , The penetration of all nodes must be 0
If the above two conditions are met at the same time , Just go back to true
- Time complexity O ( n + m ) O(n + m) O(n+m), among n n n Is the length of the arrangement , m m m yes s e q u e n c e s sequences sequences The number of elements in
- Spatial complexity O ( n + m ) O(n + m) O(n+m)
AC Code
C++
class Solution {
public:
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
int n = nums.size();
vector<vector<int>> from(n + 1);
vector<int> inDegree(n + 1, 0);
for (vector<int>& v : sequences) {
for (int i = 1; i < v.size(); i++) {
// v[i - 1] → v[i]
from[v[i - 1]].push_back(v[i]);
inDegree[v[i]]++;
}
}
queue<int> zero;
for (int i = 1; i <= n; i++) {
if (inDegree[i] == 0) {
zero.push(i);
}
}
while (zero.size()) {
if (zero.size() != 1)
return false;
int thisFrom = zero.front();
zero.pop();
for (int& thisTo : from[thisFrom]) {
inDegree[thisTo]--;
if (!inDegree[thisTo]) {
zero.push(thisTo);
}
}
}
for (int i = 1; i <= n; i++) {
if (inDegree[i])
return false;
}
return true;
}
};
It's not easy to make pictures , If you like it, just like it before you leave ~
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125945290
边栏推荐
- Flink Exactly-Once 投递实现浅析
- SQLZOO——SELECT from Nobel Tutorial
- Time frequency domain analysis of 20220721 integral link
- 使用kail破解wifi密码
- PCL:多直線擬合(RANSAC)
- 《通信软件开发与应用》课程结业报告
- ZigBee system development of Internet of things I (Introduction to ZigBee) [easy to understand]
- 银行业如何实现数字化转型风口全速启航
- 【2013】【论文笔记】太赫兹波段纳米颗粒表面增强拉曼——
- Spark installation and startup
猜你喜欢

Rhcsa Notes 6

Installation and use of flame graphs

如何成为建模师?工业建模和游戏建模哪一个更加吃香?
![[heavyweight] focusing on the terminal business of securities companies, Borui data released a new generation of observable platform for the core business experience of securities companies' terminals](/img/28/8d9f33ad6f54d6344429a687a7d1d7.png)
[heavyweight] focusing on the terminal business of securities companies, Borui data released a new generation of observable platform for the core business experience of securities companies' terminals

面试官:你觉得你最大的缺点是什么?

Does anyone get a job by self-study modeling? Don't let these thoughts hurt you

一文详解:TMP1750芯片 三通道线性LED驱动器

大神“魔改”AirPods,配备USB-C接口,3D打印外壳让维修更容易

使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏

学次世代建模是场景好还是角色好?选对职业薪资多一半
随机推荐
大神“魔改”AirPods,配备USB-C接口,3D打印外壳让维修更容易
接口测试概述
Rhcsa note 4
PCL: multi line fitting (RANSAC)
Pessimistic lock and optimistic lock
VS2010一个解决方案下新建多个项目出现的问题和方法
Rhcsa notes 7
Modeling at the beginning of learning is very confused, how to learn next generation role modeling?
从业务开发中学习和理解架构设计
元胞数组处理
LeetCode 0131. 分割回文串
Digital signal processing experiment (I)
【2018】【论文笔记】石墨烯场效应管及【1】——GFETs的种类和原理,GFETs特性,GFETs在太赫兹中的应用和原理
How does Apache, the world's largest open source foundation, work?
Block encryption mode ECB, CBC, PCBC, CFB, OFB, CTR
[attack and defense world web] difficulty four-star 12 point advanced question: cat
String length function strlen().. String function header file string.h "suggestions collection"
【2020】【论文笔记】相变材料与超表面——
Rhcsa notes 5
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer