LeetCode-444-序列重建

LeetCode-444-序列重建

给定一个长度为 n 的整数数组 nums ,其中 nums 是范围为 [1,n] 的整数的排列。还提供了一个 2D 整数数组 sequences ,其中 sequences[i]nums 的子序列。
检查 nums 是否是唯一的最短 超序列 。最短 超序列长度最短 的序列,并且所有序列 sequences[i] 都是它的子序列。对于给定的数组 sequences ,可能存在多个有效的 超序列

  • 例如,对于 sequences = [[1,2],[1,3]] ,有两个最短的 超序列[1,2,3][1,3,2]
  • 而对于 sequences = [[1,2],[1,3],[1,2,3]] ,唯一可能的最短 超序列[1,2,3][1,2,3,4] 是可能的超序列,但不是最短的。

如果 nums 是序列的唯一最短 超序列 ,则返回 true ,否则返回 false
子序列 是一个可以通过从另一个序列中删除一些元素或不删除任何元素,而不改变其余元素的顺序的序列。

示例 1:

输入:nums = [1,2,3], sequences = [[1,2],[1,3]]
输出:false
解释:有两种可能的超序列:[1,2,3]和[1,3,2]。
序列 [1,2] 是[1,2,3]和[1,3,2]的子序列。
序列 [1,3] 是[1,2,3]和[1,3,2]的子序列。
因为 nums 不是唯一最短的超序列,所以返回false。

示例 2:

输入:nums = [1,2,3], sequences = [[1,2]]
输出:false
解释:最短可能的超序列为 [1,2]。
序列 [1,2] 是它的子序列:[1,2]。
因为 nums 不是最短的超序列,所以返回false。

示例 3:

输入:nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
输出:true
解释:最短可能的超序列为[1,2,3]。
序列 [1,2] 是它的一个子序列:[1,2,3]。
序列 [1,3] 是它的一个子序列:[1,2,3]。
序列 [2,3] 是它的一个子序列:[1,2,3]。
因为 nums 是唯一最短的超序列,所以返回true。

提示:

  • n == nums.length
  • 1 <= n <= 104
  • nums[1, n] 范围内所有整数的排列
  • 1 <= sequences.length <= 104
  • 1 <= sequences[i].length <= 104
  • 1 <= sum(sequences[i].length) <= 105
  • 1 <= sequences[i][j] <= n
  • sequences 的所有数组都是 唯一
  • sequences[i]nums 的一个子序列
class Solution {
    /**
     * 构建图, 然后判断是否存在唯一的一条拓扑序列
     *
     * 问题1: 子序列中是否包含所有元素?
     *      如果子序列不包含所有元素, 那么在查找入度为0的节点时会有多个入度为0的节点, 无法确定唯一的超序列
     * 问题2: 是否会构成环?
     *      不会, 每一个子序列都是按照数组顺序排列的
     */
    public boolean sequenceReconstruction(int[] nums, int[][] sequences) {
        int len = nums.length;
        // 图
        HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();
        // 记录顶点入度
        int[] inDeg = new int[len + 1];

        // 构建图
        for (int[] sequence : sequences) {
            int size = sequence.length;
            for (int i = 1; i < size; i++) {
                int from = sequence[i - 1];
                int to = sequence[i];
                if (graph.containsKey(from) && graph.get(from).contains(to)) {
                    continue;
                }
                graph.putIfAbsent(from, new HashSet<>());
                graph.get(from).add(to);
                inDeg[to]++;
            }
        }

        // 找到入度为0的顶点
        Queue<Integer> queue = new ArrayDeque<>();
        for (int i = 1; i <= len; i++) {
            if (inDeg[i] == 0) {
                queue.add(i);
            }
        }

        // 找拓扑序列
        while (!queue.isEmpty()) {
            // 路径出现分叉
            if (queue.size() > 1) {
                return false;
            }
            int from = queue.poll();
            HashSet<Integer> tos = graph.get(from);
            if (tos == null) {
                continue;
            }
            for (Integer to : tos) {
                inDeg[to]--;
                if (inDeg[to] == 0) {
                    queue.add(to);
                }
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值