[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
这道题给了我们一个序列org,又给我们了一些子序列seqs,问这些子序列能否唯一的重建出原序列。能唯一重建的意思就是任意两个数字的顺序必须是一致的,不能说在一个子序列中1在4的后面,但是在另一个子序列中1在4的前面,这样就不是唯一的了。还有一点就是,子序列seqs中不能出现其他的数字,就是说必须都是原序列中的数字。那么我们可以用了一个一维数组pos来记录org中每个数字对应的位置,然后用一个flags数字来标记当前数字和其前面一个数字是否和org中的顺序一致,用cnt来标记还需要验证顺序的数字的个数,初始化cnt为n-1,因为n个数字只需要验证n-1对顺序即可,然后我们先遍历一遍org,将每个数字的位置信息存入pos中,然后再遍历子序列中的每一个数字,还是要先判断数字是否越界,然后我们取出当前数字cur,和其前一位置上的数字pre,如果在org中,pre在cur之后,那么直接返回false。否则我们看如果cur的顺序没被验证过,而且pre是在cur的前一个,那么标记cur已验证,且cnt自减1,最后如果cnt为0了,说明所有顺序被成功验证了,参见代码如下:
解法一:
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
if (seqs.empty()) return false;
int n = org.size(), cnt = n - ;
vector<int> pos(n + , ), flags(n + , );
bool existed = false;
for (int i = ; i < n; ++i) pos[org[i]] = i;
for (auto& seq : seqs) {
for (int i = ; i < seq.size(); ++i) {
existed = true;
if (seq[i] <= || seq[i] > n) return false;
if (i == ) continue;
int pre = seq[i - ], cur = seq[i];
if (pos[pre] >= pos[cur]) return false;
if (flags[cur] == && pos[pre] + == pos[cur]) {
flags[cur] = ; --cnt;
}
}
}
return cnt == && existed;
}
};
下面这种方法跟上面的方法大同小异,用两个哈希表来代替了上面的数组和变量,其中m为数字和其位置之间的映射,pre为当前数字和其前一个位置的数字在org中的位置之间的映射。跟上面的方法的不同点在于,当遍历到某一个数字的时候,我们看当前数字是否在pre中有映射,如果没有的话,我们建立该映射,注意如果是第一个位置的数字的话,其前面数字设为-1。如果该映射存在的话,我们对比前一位数字在org中的位置和当前的映射值的大小,取其中较大值。最后我们遍历一遍org,看每个数字的映射值是否是前一个数字的位置,如果有不是的返回false,全部验证成功返回true,参见代码如下:
解法二:
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
unordered_map<int, int> m, pre;
for (int i = ; i < org.size(); ++i) m[org[i]] = i;
for (auto& seq : seqs) {
for (int i = ; i < seq.size(); ++i) {
if (!m.count(seq[i])) return false;
if (i > && m[seq[i - ]] >= m[seq[i]]) return false;
if (!pre.count(seq[i])) {
pre[seq[i]] = (i > ) ? m[seq[i - ]] : -;
} else {
pre[seq[i]] = max(pre[seq[i]], (i > ) ? m[seq[i - ]] : -);
}
}
}
for (int i = ; i < org.size(); ++i) {
if (pre[org[i]] != i - ) return false;
}
return true;
}
};
参考资料:
https://leetcode.com/problems/sequence-reconstruction/submissions/
https://discuss.leetcode.com/topic/65737/concise-c-solution-inspired-by-previous-great-solutions
https://discuss.leetcode.com/topic/65961/simple-solution-one-pass-using-only-array-c-92ms-java-16ms
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Sequence Reconstruction 序列重建的更多相关文章
- Leetcode: Sequence Reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- OpenJudge 由中根顺序和后根序列重建二叉树
题目内容: 我们知道如何按照三种深度优先次序来周游一棵二叉树,来得到中根序列.前根序列和后根序列.反过来,如果给定二叉树的中根序列和后根序列,或者给定中根序列和前根序列,可以重建一二叉树.本题输入一棵 ...
- python3 第十七章 - sequence(序列)
之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...
- EF中创建、使用Oracle数据库的Sequence(序列)功能
** 背景 ** 项目中订单号原来的生成规则由日期加随机数组成,后期需求决定将订单号生成规则更改为生成日期加当天当前订单数. 每天的订单数都是从0开始的,每生成一个订单,订单数就应该加1.订单数应该是 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- [LeetCode]444. Sequence Reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- [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 &q ...
- [LeetCode] 936. Stamping The Sequence 戳印序列
You want to form a `target` string of lowercase letters. At the beginning, your sequence is target.l ...
随机推荐
- 【分布式】Zookeeper使用--开源客户端
一.前言 上一篇博客已经介绍了如何使用Zookeeper提供的原生态Java API进行操作,本篇博文主要讲解如何通过开源客户端来进行操作. 二.ZkClient ZkClient是在Zookeepe ...
- [未完成]scikit-learn一般实例之九:用于随机投影嵌入的Johnson–Lindenstrauss lemma边界
Johnson–Lindenstrauss 引理表明任何高维数据集均可以被随机投影到一个较低维度的欧氏空间,同时可以控制pairwise距离的失真. 理论边界 由一个随机投影P所引入的失真是确定的,这 ...
- 02 button的练习
private void button1_Click(object sender, EventArgs e) { MessageBox.Show("我也喜欢你!"); //if ( ...
- 浅谈Collection集合
俗话说:一个东西,一件事都离不开三句话:"是什么,为什么,怎么办" 集合是什么: 集合简单的说一个数组集合的高级体现,用来存储数据或对象的容器: 集合为什么存在: 集合只是体现了对 ...
- mybatis hibernate比较
开发速度: 如果一个项目中用到的复杂的查询基本没有,就是简单的增删该查,这样选择hibernate效率就很快了,因为基本的sql语句已经被封装好了,根本不用去写sql语句,但是对于一个大型项目,复杂语 ...
- Angular的自定义指令以及实例
本文章已收录于: AngularJS知识库 分类: javascript(55) http://www.cnblogs.com/xiaoxie53/p/5058198.html 前面的文章介 ...
- jQ图片列表光标移动动画
本效果使用jQuery和CSS实现了图片列表,当鼠标移入时图片向左微动,移出则复原. 效果展示: http://hovertree.com/texiao/jquery/88/ 效果图如下: 其中的jQ ...
- HTML学习(二)进阶篇
在博客园中有许多大神对HTML超文本标记语言写了很多内容,总结了很多知识,这里对我看到的博客文章, 所学到的知识,做一个总结. 一)列表和表格 dl→definition list(定义列表),见备 ...
- Linux2.6内核协议栈系列--TCP协议1.发送
在介绍tcp发送函数之前得先介绍很关键的一个结构sk_buff,在linux中,sk_buff结构代表了一个报文: 然后见发送函数源码,这里不关注硬件支持的分散-聚集: /* sendmsg系统调用在 ...
- (十一)Maven远程仓库的各种配置
1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...