LeetCode 1764 Form Array by Concatenating Subarrays of Another Array (dfs)

244 篇文章 0 订阅
16 篇文章 0 订阅

You are given a 2D integer array groups of length n. You are also given an integer array nums.

You are asked if you can choose n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before the ith subarray in nums (i.e. the subarrays must be in the same order as groups).

Return true if you can do this task, and false otherwise.

Note that the subarrays are disjoint if and only if there is no index k such that nums[k] belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.

Example 1:

Input: groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
Output: true
Explanation: You can choose the 0th subarray as [1,-1,0,1,-1,-1,3,-2,0] and the 1st one as [1,-1,0,1,-1,-1,3,-2,0].
These subarrays are disjoint as they share no common nums[k] element.

Example 2:

Input: groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
Output: false
Explanation: Note that choosing the subarrays [1,2,3,4,10,-2] and [1,2,3,4,10,-2] is incorrect because they are not in the same order as in groups.
[10,-2] must come before [1,2,3,4].

Example 3:

Input: groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
Output: false
Explanation: Note that choosing the subarrays [7,7,1,2,3,4,7,7] and [7,7,1,2,3,4,7,7] is invalid because they are not disjoint.
They share a common elements nums[4] (0-indexed).

Constraints:

  • groups.length == n
  • 1 <= n <= 103
  • 1 <= groups[i].length, sum(groups[i].length) <= 103
  • 1 <= nums.length <= 103
  • -107 <= groups[i][j], nums[k] <= 107

题目链接:Loading...

题目大意:问从nums是否可以按顺序选出groups里的数组,要求按照groups中的数组顺序,数组中元素在nums中要连续且不存在覆盖的情况

题目分析:模拟一下即可,找到当前nums第一个和当前group第一个元素相等的元素位置开始遍历

1ms,时间击败70.24%

class Solution {
    
    boolean find = false;
    
    void dfs(int groupPos, int[][] groups, int numPos, int[] nums) {
        if (find) {
            return;
        }
        if (groupPos == groups.length) {
            if (numPos <= nums.length) {
                find = true;
            }
            return;
        }
        if (numPos >= nums.length) {
            return;
        }
        int i = numPos;
        while (i < nums.length && nums[i] != groups[groupPos][0]) {
            i++;
        }
        if (i >= nums.length) {
            return;
        }
        boolean ok = true;
        int k = i, j = 0;
        while (k < nums.length && j < groups[groupPos].length) {
            if (nums[k++] != groups[groupPos][j++]) {
                ok = false;
                break;
            }
        }
        if (j != groups[groupPos].length) {
            ok = false;
        }
        if (!ok) {
            dfs(groupPos, groups, i + 1, nums);
        } else {
            dfs(groupPos + 1, groups, k, nums);
        }
    }
    
    public boolean canChoose(int[][] groups, int[] nums) {
        dfs(0, groups, 0, nums);
        return find;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值