{"id":7194,"date":"2020-08-02T12:18:39","date_gmt":"2020-08-02T19:18:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7194"},"modified":"2020-08-02T12:19:08","modified_gmt":"2020-08-02T19:19:08","slug":"leetcode-1536-minimum-swaps-to-arrange-a-binary-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1536-minimum-swaps-to-arrange-a-binary-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1536. Minimum Swaps to Arrange a Binary Grid"},"content":{"rendered":"\n<p>Given an&nbsp;<code>n&nbsp;x n<\/code>&nbsp;binary&nbsp;<code>grid<\/code>, in one step you can choose two&nbsp;<strong>adjacent rows<\/strong>&nbsp;of the grid and swap them.<\/p>\n\n\n\n<p>A grid is said to be&nbsp;<strong>valid<\/strong>&nbsp;if all the cells above the main diagonal are&nbsp;<strong>zeros<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of steps<\/em>&nbsp;needed to make the grid valid, or&nbsp;<strong>-1<\/strong>&nbsp;if the grid cannot be valid.<\/p>\n\n\n\n<p>The main diagonal of a grid is the diagonal that starts at cell&nbsp;<code>(1, 1)<\/code>&nbsp;and ends at cell&nbsp;<code>(n, n)<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/28\/fw.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,0,1],[1,1,0],[1,0,0]]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/16\/e2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> All rows are similar, swaps have no effect on the grid.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/16\/e3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0,0],[1,1,0],[1,1,1]]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= n&nbsp;&lt;= 200<\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bubble Sort<\/strong><\/h2>\n\n\n\n<p>Counting how many tailing zeros each row has.<br>Then input<br>[0, 0, 1]<br>[1, 1, 0]<br>[1, 0, 0]<br>becomes [0, 1, 2]<br>For i-th row, it needs n &#8211; i &#8211; 1 tailing zeros.<br>We need to find the <strong>first<\/strong> row that has <strong>at least<\/strong> n &#8211; i &#8211; 1 tailing zeros and bubbling it up to the i-th row. This process is very similar to bubble sort.<br>[0, 1, <strong>2<\/strong>] -&gt; [0, <strong>2<\/strong>, 1] -&gt; [<strong>2<\/strong>, 0, 1]<br>[2, 0, <strong>1<\/strong>] -&gt; [2, <strong>1<\/strong>, 0]<br>Total 3 swaps.<br><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minSwaps(vector<vector<int>>& grid) {\n    const int n = grid.size();\n    vector<int> zeros;\n    for (const auto& row : grid) {\n      int zero = 0;\n      for (int i = n - 1; i >= 0 && row[i] == 0; --i) \n        ++zero;\n      zeros.push_back(zero);\n    }\n    int ans = 0;\n    for (int i = 0; i < n; ++i) {\n      int j = i;\n      int z = n - i - 1; \/\/ the i-th needs n - i - 1 zeros\n      \/\/ Find first row with at least z tailing zeros.\n      while (j < n &#038;&#038; zeros[j] < z) ++j;\n      if (j == n) return -1;\n      while (j > i) {\n        zeros[j] = zeros[j - 1];\n        --j;\n        ++ans;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;n&nbsp;x n&nbsp;binary&nbsp;grid, in one step you can choose two&nbsp;adjacent rows&nbsp;of the grid and swap them. A grid is said to be&nbsp;valid&nbsp;if all the cells&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7194","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7194","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=7194"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7194\/revisions"}],"predecessor-version":[{"id":7196,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7194\/revisions\/7196"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}