java.lang.Object
g1701_1800.s1764_form_array_by_concatenating_subarrays_of_another_array.Solution

public class Solution extends Object
1764 - Form Array by Concatenating Subarrays of Another Array.<p>Medium</p> <p>You are given a 2D integer array <code>groups</code> of length <code>n</code>. You are also given an integer array <code>nums</code>.</p> <p>You are asked if you can choose <code>n</code> <strong>disjoint</strong> subarrays from the array <code>nums</code> such that the <code>i<sup>th</sup></code> subarray is equal to <code>groups[i]</code> ( <strong>0-indexed</strong> ), and if <code>i > 0</code>, the <code>(i-1)<sup>th</sup></code> subarray appears <strong>before</strong> the <code>i<sup>th</sup></code> subarray in <code>nums</code> (i.e. the subarrays must be in the same order as <code>groups</code>).</p> <p>Return <code>true</code> <em>if you can do this task, and</em> <code>false</code> <em>otherwise</em>.</p> <p>Note that the subarrays are <strong>disjoint</strong> if and only if there is no index <code>k</code> such that <code>nums[k]</code> belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> You can choose the 0<sup>th</sup> subarray as [1,-1,0, <strong>1,-1,-1</strong> ,3,-2,0] and the 1<sup>st</sup> one as [1,-1,0,1,-1,-1, <strong>3,-2,0</strong> ]. These subarrays are disjoint as they share no common nums[k] element.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Note that choosing the subarrays [<strong>1,2,3,4</strong> ,10,-2] and [1,2,3,4, <strong>10,-2</strong> ] is incorrect because they are not in the same order as in groups. [10,-2] must come before [1,2,3,4].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Note that choosing the subarrays [7,7, <strong>1,2,3</strong> ,4,7,7] and [7,7,1,2, <strong>3,4</strong> ,7,7] is invalid because they are not disjoint. They share a common elements nums[4] (0-indexed).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>groups.length == n</code></li> <li><code>1 <= n <= 10<sup>3</sup></code></li> <li><code>1 <= groups[i].length, sum(groups[i].length) <= 10<sup>3</sup></code></li> <li><code>1 <= nums.length <= 10<sup>3</sup></code></li> <li><code>-10<sup>7</sup> <= groups[i][j], nums[k] <= 10<sup>7</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canChoose

      public boolean canChoose(int[][] groups, int[] nums)