当前位置:网站首页>Sword finger offer II 115: reconstruction sequence
Sword finger offer II 115: reconstruction sequence
2022-07-29 06:57:00 【Chrysanthemum headed bat】
The finger of the sword Offer II 115: Reconstruction sequence
subject
Topic linking
Given a length of n Array of integers for nums , among nums Is in the range of [1,n] The arrangement of integers . One is also provided 2D An array of integers sequences , among sequences[i] yes nums The subsequence .
Check nums Is it the only shortest Supersequence . The shortest Supersequence yes The shortest length Sequence , And all sequences sequences[i] Are its subsequences . For a given array sequences , There may be multiple valid Supersequence .
for example , about sequences = [[1,2],[1,3]] , There are two shortest Supersequence ,[1,2,3] and [1,3,2] .
And for sequences = [[1,2],[1,3],[1,2,3]] , The only possible shortest Supersequence yes [1,2,3] .[1,2,3,4] Is a possible supersequence , But not the shortest .
If nums Is the only shortest sequence Supersequence , Then return to true , Otherwise return to false .
Subsequence One can delete some elements from another sequence or not delete any elements , A sequence that does not change the order of the remaining elements .
Example 1:
Input :nums = [1,2,3], sequences = [[1,2],[1,3]]
Output :false
explain : There are two possible supersequences :[1,2,3] and [1,3,2].
Sequence [1,2] yes [1,2,3] and [1,3,2] The subsequence .
Sequence [1,3] yes [1,2,3] and [1,3,2] The subsequence .
because nums Is not the only shortest supersequence , So back false.
Example 2:
Input :nums = [1,2,3], sequences = [[1,2]]
Output :false
explain : The shortest possible supersequence is [1,2].
Sequence [1,2] Is its subsequence :[1,2].
because nums Not the shortest supersequence , So back false.
Example 3:
Input :nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
Output :true
explain : The shortest possible supersequence is [1,2,3].
Sequence [1,2] It's a subsequence of it :[1,2,3].
Sequence [1,3] It's a subsequence of it :[1,2,3].
Sequence [2,3] It's a subsequence of it :[1,2,3].
because nums Is the only shortest supersequence , So back true.
Problem solving
Method 1 : A topological sort
Reference link
We put sequences As a directed graph , If sorted by topology , The degree of 0 There is only one point , It must be the shortest supersequence , Otherwise, it's not .
This question can also be understood as , use sequences Can it be transformed into a unique sequence .
class Solution {
public:
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
int n=nums.size();
vector<vector<int>> g(n+1);
vector<int> indeg(n+1);
for(vector<int>& seq:sequences){
for(int i=0;i<seq.size()-1;i++){
int x=seq[i],y=seq[i+1];
g[x].push_back(y);
indeg[y]++;
}
}
queue<int> q;
for(int x=1;x<=n;x++){
if(indeg[x]==0) q.push(x);
}
while(!q.empty()){
if(q.size()>1) return false;
int x=q.front();
q.pop();
for(int y:g[x]){
if(--indeg[y]==0){
q.push(y);
}
}
}
return true;
}
};
边栏推荐
- 【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
- 竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
- Recurrent neural network RNN
- Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)
- 量子机器学习中的安全性问题
- Jetpack Compose 中的键盘处理
- 王树尧老师运筹学课程笔记 04 线性代数基础
- IDEA找不到Database解决方法
- ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
- Shallow reading of condition object source code
猜你喜欢
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
STP spanning tree principle and example of election rules
CNN convolutional neural network
【讲座笔记】如何在稀烂的数据中做深度学习?
Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
Unity免费元素特效推荐
How to write controller layer code gracefully?
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
循环神经网络RNN
随机推荐
微信小程序的反编译
SS command details
【笔记】The art of research(明白问题的重要性)
Federal learning backdoor attack summary (2019-2022)
Database multi table query joint query add delete modify query
Teacher wangshuyao's notes on operations research 02 fundamentals of advanced mathematics
LDAP brief description and unified authentication description
数据库持久化+JDBC数据库连接
损失函数——交叉熵损失函数
10道面试常问JVM题
DBAsql面试题
量子机器学习中的安全性问题
Shallow reading of condition object source code
NLP-分词
Teacher wangshuyao wrote the notes of operations research course 00 in the front
Let the computer run only one program setting
C语言数据类型
Idea cannot find a database solution
Enterprise manager cannot connect to the database instance in Oracle10g solution
好文佳句摘录