LeetCode 444. Sequence Reconstruction

来源:互联网 发布:自制西门子编程线 编辑:程序博客网 时间:2024/06/05 16:33

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 1n104. 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思路:
1. 这是一道两体问题,即:输入是两个对象,一个是org,一个是seqs,要求寻找两者之间的联系。首先,我们遍历seqs,把其某些特征保存下来,然后再遍历org,提取同样的特征,然后从前面的特征中查询即可!
2. 具体的做法:先将seq中每个数字前面的数字记录下来,保证supersequence中数字相对位置关系正确,而且都定义过的。比如:seqs=[[1,2],[1,3]],1的前面没有,可以用0表示, 2的前面是1,3的前面是;然后遍历org,看是否所有的前后关系所形成的特征都能在之前记录的特征库中查询,例如:org=[1,2,3],1前面没有,正确;2前面是1,也正确;3的前面是2,不正确,因为在上面的特征库里我们只记录了3的前面是1没有2,所以不正确!
3. 上面的方法还不完全正确,例如:seqs=[[1,4],[4,1]],org=[1,4].我们根据上文做法:1的前面有0,4,4的前面有1,0,而org里需要1前面有0,正确;4前面有1,正确。但显然这个org不是seqs的super sequence。因为,1在4前,又再4后。所以,除了检查org里前后相继的特征是否存在,还需要检查某个数前面所有的数是否都存在!
4. 这样检查就费劲了!例如:seqs=[[1,3],[2,3]],org=[1,2,3],3前面有{1,2}.所以在org遇到3,就判断前面{1,2}是否等于{1,2}。看来这个时候,需要我们反过来思考。根据3前面的数字1,查询org知道1的位置是0,而3前面的数字2对于的org中2的位置是1,所以,我们直接选位置最大的数即可,从而不用保存和比较3前面所有的数。这个方法可行的原因,是每次需判断3在org里的坐标是否比1、2在org的坐标大。这里利用了数据相对位置在seqs和org中永远保持不变。 参考http://www.cnblogs.com/grandyang/p/6032498.html 

//方法1:用unordered_map保存seqs中每个数的前面的数的最大位置。//用vector保持org中每个数对应的位置,每次判断seqs中每个数和这个数前面数的相对位置关系是否和org中位置关系一直!!bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {    //    int maxNum=org.size();    vetor<int> pos(maxNum);    unordered_map<int,int> priorPos;    //保持org中每个数对应的位置,即:根据数值大小找到相对应的位置,相当于map,给出数和坐标的映射    for(int i=0;i<maxNum;i++) pos[org[i]]=i;    for(auto seq:seqs){        for(int i=0;i<seq.size();i++){            if(seq[i]>maxNum||seq[i]<=0)                 return false;//越界            //每次判断seqs中相对位置是否和org一致!!            if(i>0&&pos[seq[i]]<=pos[seq[i-1]])                return false;            if(!priorPos.count(seq[i])){//第一次存                priorPos[seq[i]]=i>0?pos[seq[i-1]]:-1;//i=0,直接存-1,因为没有pre data            }else{                priorPos[seq[i]]=max(priorPos[seq[i]],i>0?pos[seq[i-1]]:-1);                //存最大的坐标,例如:seqs=[[1,3],[2,3]],org=[1,2,3]。3的priorPos第一次是1在org中的位置=0;然后发现3的前面2在org中位置更大=1,所以替换成1!               }        }       }       for(int i=0;i<org.size();i++)        if(priorPos[org[i]]!=i-1) return false;    return true;}//方法2:http://www.cnblogs.com/grandyang/p/6032498.html//不用unordered_map来保持,而用vector,思路大同小异!
0 0
原创粉丝点击