当前位置:网站首页>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;
}
};
边栏推荐
- C语言数据类型
- Shallow reading of condition object source code
- 10 frequently asked JVM questions in interviews
- Database multi table query joint query add delete modify query
- 【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
- Share some tips for better code, smooth coding and improve efficiency
- 【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)
- 线程 - 线程安全 - 线程优化
- 二次元卡通渲染——进阶技巧
- Neuralcf neural collaborative filtering network
猜你喜欢

IDEA找不到Database解决方法

Why does 5g N2 interface control plane use SCTP protocol?

Ping principle

【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读

Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation

联邦学习后门攻击总结(2019-2022)

王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)

游戏资产的革命

Unity免费元素特效推荐

Apisik health check test
随机推荐
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
Salesforce中过滤器Filter使用的相对日期
Computer right mouse click always turn around what's going on
Mutual conversion between Base64 and file
Enterprise manager cannot connect to the database instance in Oracle10g solution
API for using the new date class of instant
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)
基于噪声伪标签和对抗性学习的医学图像分割注释有效学习
STP spanning tree principle and example of election rules
Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
Teacher Wu Enda's machine learning course notes 00 are written in the front
吴恩达老师机器学习课程笔记 05 Octave教程
循环神经网络RNN
Security in quantum machine learning
Teacher wangshuyao wrote the notes of operations research course 00 in the front
Phantom reference virtual reference code demonstration
【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice