[LeetCode] Sequence Reconstruction 序列重建

Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.

Example 1:

Input:
org: [1,2,3], seqs: [[1,2],[1,3]]

Output:
false

Explanation:
[1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.

Example 2:

Input:
org: [1,2,3], seqs: [[1,2]]

Output:
false

Explanation:
The reconstructed sequence can only be [1,2].

Example 3:

Input:
org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]

Output:
true

Explanation:
The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct 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

s

时间: 2024-11-07 19:47:57

[LeetCode] Sequence Reconstruction 序列重建的相关文章

Leetcode: Sequence Reconstruction

Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the se

OpenJudge 由中根顺序和后根序列重建二叉树

[题解] 后根序列的最后一个元素即为二叉树的树根root.root将中根序列分为两部分,左半边是左子树的中根序列,而右半边则是右部分的中根序列.同时后根序列依照左子树和右子树节点数也可以被分为左子树的后根序列和右子树的后根序列.于是便可依此递归地按左右子树的后根.中根序列重建子树,最终重建二叉树. [代码] 1 #include <iostream> 2 using namespace std; 3 /*build BT from its inorder and postorder*/ 4 5

根据前序和中序列 重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数. 输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列.

【LeetCode每天一题】Permutation Sequence(排列序列)

The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence for n = 3: "123" "132" "213" "231" "312" "321&q

[LeetCode] Queue Reconstruction by Height 根据高度重建队列

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h

[LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列

We are given?S, a length?n?string of characters from the set?{'D', 'I'}. (These letters stand for "decreasing" and "increasing".) A?valid permutation?is a permutation?P[0], P[1], ..., P[n]?of integers?{0, 1, ..., n}, such that for all?

Cocos2d-x之Sequence动作序列执行

//实现动作的序列执行,先执行移动,然后在执行旋转 label->runAction(Sequence::create(MoveBy::create(1, Point(100,100)), RotateBy::create(1, 360),NULL));

Sequence Reconstruction

Only Path :判断是否只存在一个拓扑排序的序列 只需要保证队列中一直最多只有1个元素即可 tricky part:1) org has all the nodes. That is why each time there should only be 1 element in the queue. If it is not, one node is missing. The normal Topology just checked whether all the node could b

依据前序和中序列 重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.如果输入的前序遍历和中序遍历的结果中都不含反复的数字.比如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}.则重建二叉树并输出它的后序遍历序列. 输入: 输入可能包括多个測试例子.对于每一个測试案例, 输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数. 输入的第二行包含n个整数(当中每一个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列