{"id":8131,"date":"2021-02-20T16:32:23","date_gmt":"2021-02-21T00:32:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8131"},"modified":"2021-02-20T17:39:52","modified_gmt":"2021-02-21T01:39:52","slug":"leetcode-1764-form-array-by-concatenating-subarrays-of-another-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1764. Form Array by Concatenating Subarrays of Another Array"},"content":{"rendered":"\n<p>You are given a 2D integer array&nbsp;<code>groups<\/code>&nbsp;of length&nbsp;<code>n<\/code>. You are also given an integer array&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>You are asked if you can choose&nbsp;<code>n<\/code>&nbsp;<strong>disjoint&nbsp;<\/strong>subarrays from the array&nbsp;<code>nums<\/code>&nbsp;such that the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;subarray is equal to&nbsp;<code>groups[i]<\/code>&nbsp;(<strong>0-indexed<\/strong>), and if&nbsp;<code>i &gt; 0<\/code>, the&nbsp;<code>(i-1)<sup>th<\/sup><\/code>&nbsp;subarray appears&nbsp;<strong>before<\/strong>&nbsp;the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;subarray in&nbsp;<code>nums<\/code>&nbsp;(i.e. the subarrays must be in the same order as&nbsp;<code>groups<\/code>).<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if you can do this task, and<\/em>&nbsp;<code>false<\/code>&nbsp;<em>otherwise<\/em>.<\/p>\n\n\n\n<p>Note that the subarrays are&nbsp;<strong>disjoint<\/strong>&nbsp;if and only if there is no index&nbsp;<code>k<\/code>&nbsp;such that&nbsp;<code>nums[k]<\/code>&nbsp;belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]\n<strong>Output:<\/strong> true\n<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>].\nThese subarrays are disjoint as they share no common nums[k] element.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]\n<strong>Output:<\/strong> false\n<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.\n[10,-2] must come before [1,2,3,4].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]\n<strong>Output:<\/strong> false\n<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.\nThey share a common elements nums[4] (0-indexed).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>groups.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>1 &lt;= groups[i].length, sum(groups[i].length) &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>1 &lt;= nums.length &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>-10<sup>7<\/sup>&nbsp;&lt;= groups[i][j], nums[k] &lt;= 10<sup>7<\/sup><\/code><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2?)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canChoose(vector<vector<int>>& groups, vector<int>& nums) {\n    const int n = nums.size();\n    int s = 0;\n    for (const auto& g : groups) {\n      bool found = false;\n      for (int i = s; i <= n - g.size(); ++i)\n        if (equal(begin(g), end(g), begin(nums) + i)) {\n          s = i + g.size();\n          found = true;\n          break;\n        }\n      if (!found) return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D integer array&nbsp;groups&nbsp;of length&nbsp;n. You are also given an integer array&nbsp;nums. You are asked if you can choose&nbsp;n&nbsp;disjoint&nbsp;subarrays from the array&nbsp;nums&nbsp;such&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,572,410,97,4],"class_list":["post-8131","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-kmp","tag-matching","tag-prefix","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8131"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8131\/revisions"}],"predecessor-version":[{"id":8133,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8131\/revisions\/8133"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}