{"id":6833,"date":"2020-05-30T16:09:01","date_gmt":"2020-05-30T23:09:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6833"},"modified":"2020-05-30T16:10:12","modified_gmt":"2020-05-30T23:10:12","slug":"leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1460. Make Two Arrays Equal by Reversing Sub-arrays"},"content":{"rendered":"\n<p>Given two integer arrays of equal length&nbsp;<code>target<\/code>&nbsp;and&nbsp;<code>arr<\/code>.<\/p>\n\n\n\n<p>In one step, you can select any&nbsp;<strong>non-empty sub-array<\/strong>&nbsp;of&nbsp;<code>arr<\/code>&nbsp;and reverse it. You are allowed to make any number of steps.<\/p>\n\n\n\n<p>Return&nbsp;<em>True<\/em>&nbsp;if you can make&nbsp;<code>arr<\/code>&nbsp;equal to&nbsp;<code>target<\/code>, or&nbsp;<em>False<\/em>&nbsp;otherwise.<\/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> target = [1,2,3,4], arr = [2,4,1,3]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> You can follow the next steps to convert arr to target:\n1- Reverse sub-array [2,4,1], arr becomes [1,4,2,3]\n2- Reverse sub-array [4,2], arr becomes [1,2,4,3]\n3- Reverse sub-array [4,3], arr becomes [1,2,3,4]\nThere are multiple ways to convert arr to target, this is not the only way to do so.\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> target = [7], arr = [7]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> arr is equal to target without any reverses.\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> target = [1,12], arr = [12,1]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> target = [3,7,9], arr = [3,7,11]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> arr doesn't have value 9 and it can never be converted to target.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> target = [1,1,1,1,1], arr = [1,1,1,1,1]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>target.length == arr.length<\/code><\/li><li><code>1 &lt;= target.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= target[i] &lt;= 1000<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>target and arr must have same elements.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1001)<\/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 canBeEqual(vector<int>& target, vector<int>& arr) {\n    vector<int> t(1001);\n    vector<int> a(1001);\n    for (int i = 0; i < target.size(); ++i) {\n      ++t[target[i]];\n      ++a[arr[i]];\n    }\n    return t == a;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def canBeEqual(self, target: List[int], arr: List[int]) -> bool:\n    return Counter(target) == Counter(arr)  \n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two integer arrays of equal length&nbsp;target&nbsp;and&nbsp;arr. In one step, you can select any&nbsp;non-empty sub-array&nbsp;of&nbsp;arr&nbsp;and reverse it. You are allowed to make any number of&#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,288,222],"class_list":["post-6833","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-count","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6833","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=6833"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6833\/revisions"}],"predecessor-version":[{"id":6835,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6833\/revisions\/6835"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}