{"id":4962,"date":"2019-03-09T22:05:34","date_gmt":"2019-03-10T06:05:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4962"},"modified":"2019-03-09T22:05:56","modified_gmt":"2019-03-10T06:05:56","slug":"1007-minimum-domino-rotations-for-equal-row","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/1007-minimum-domino-rotations-for-equal-row\/","title":{"rendered":"\u82b1\u82b1\u9171 1007. Minimum Domino Rotations For Equal Row"},"content":{"rendered":"\n<p>In a row of dominoes,&nbsp;<code>A[i]<\/code>&nbsp;and&nbsp;<code>B[i]<\/code>&nbsp;represent the top and bottom halves of the&nbsp;<code>i<\/code>-th domino.&nbsp; (A domino is a tile with two numbers from 1 to 6 &#8211; one on each half of the tile.)<\/p>\n\n\n\n<p>We may rotate the&nbsp;<code>i<\/code>-th domino, so that&nbsp;<code>A[i]<\/code>&nbsp;and&nbsp;<code>B[i]<\/code>&nbsp;swap values.<\/p>\n\n\n\n<p>Return the minimum number of rotations so that all the values in&nbsp;<code>A<\/code>&nbsp;are the same, or all the values in&nbsp;<code>B<\/code>&nbsp;are the same.<\/p>\n\n\n\n<p>If it cannot be done, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/03\/08\/domino.png\" alt=\"\" width=\"297\" height=\"239\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>\nThe first figure represents the dominoes as given by A and B: before we do any rotations.\nIf we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.\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>A = [3,5,1,2,3], B = [3,6,3,3,4]\n<strong>Output: <\/strong>-1\n<strong>Explanation: <\/strong>\nIn this case, it is not possible to rotate the dominoes to make one row of values equal.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A[i], B[i] &lt;= 6<\/code><\/li><li><code>2 &lt;= A.length == B.length &lt;= 20000<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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, 72 ms, 14.8 MB\nclass Solution {\npublic:\n  int minDominoRotations(vector<int>& A, vector<int>& B) {\n    int ans = INT_MAX;\n    for (int r = 1; r <= 6; ++r) {\n      bool flag = true;\n      int count_a = 0;\n      int count_b = 0;\n      for (int i = 0; i < A.size(); ++i) {\n        if (A[i] != r &#038;&#038; B[i] != r) {\n          flag = false; \n          break;\n        }\n        else if (A[i] == r &#038;&#038; B[i] == r) continue;\n        else if (A[i] == r) {\n          ++count_a;\n        } else if (B[i] == r) {\n          ++count_b;\n        }\n      }\n      if (flag) ans = min(ans, min(count_a, count_b));\n    }\n    return ans == INT_MAX ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a row of dominoes,&nbsp;A[i]&nbsp;and&nbsp;B[i]&nbsp;represent the top and bottom halves of the&nbsp;i-th domino.&nbsp; (A domino is a tile with two numbers from 1 to 6&#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,177],"class_list":["post-4962","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4962","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=4962"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4962\/revisions"}],"predecessor-version":[{"id":4964,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4962\/revisions\/4964"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}