当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

线程 - 线程安全 - 线程优化

实战!聊聊如何解决MySQL深分页问题

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

NeuralCF-神经协同过滤网络

LDAP brief description and unified authentication description

C语言数据类型

【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)

JVM之垃圾回收机制(GC)

如何优雅的写 Controller 层代码?

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
随机推荐
【备忘】关于ssh为什么会失败的原因总结?下次记得来找。
【经验】通过跳板机远程连接内网服务器的相关配置
Shallow reading of shared lock source code of abstractqueuedsynchronizer (AQS)
Navicat for Oracle Cannot create oci environment
vim文本编辑器的一些使用小技巧
JVM之垃圾回收机制(GC)
vscode通过remotessh结合xdebug远程调试php解决方案
Computer right mouse click always turn around what's going on
Phantom reference virtual reference code demonstration
模拟卷Leetcode【普通】150. 逆波兰表达式求值
Ping principle
Teacher Wu Enda machine learning course notes 01 introduction
Windows 上 php 7.4 连接 oracle 配置
Execution sequence of finally and return
ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
Teacher wangshuyao's notes on operations research course 08 linear programming and simplex method (simplex method)
10道面试常问JVM题
基于Matlab解决线性规划问题
吴恩达老师机器学习课程笔记 01 引言