{"id":4379,"date":"2018-11-29T21:06:23","date_gmt":"2018-11-30T05:06:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4379"},"modified":"2019-09-29T21:44:21","modified_gmt":"2019-09-30T04:44:21","slug":"leetcode-947-most-stones-removed-with-same-row-or-column","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-947-most-stones-removed-with-same-row-or-column\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 947. Most Stones Removed with Same Row or Column"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>On a 2D plane, we place stones at some integer coordinate points.&nbsp; Each coordinate point may have at most one stone.<\/p>\n<p>Now, a&nbsp;<em>move<\/em>&nbsp;consists of removing a stone&nbsp;that shares a column or row with another stone on the grid.<\/p>\n<p>What is the largest possible number of moves we can make?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>stones = <span id=\"example-input-1-2\">[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]<\/span>\n<strong>Output: <\/strong>5\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>stones = <span id=\"example-input-2-2\">[[0,0],[0,2],[1,1],[2,0],[2,2]]<\/span>\n<strong>Output: <\/strong>3\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>stones = <span id=\"example-input-3-2\">[[0,0]]<\/span>\n<strong>Output: <\/strong>0\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= stones.length &lt;= 1000<\/code><\/li>\n<li><code>0 &lt;= stones[i][j] &lt; 10000<\/code><\/li>\n<\/ol>\n<p><script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-format=\"fluid\" data-ad-layout-key=\"-fb+5w+4e-db+86\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"2162692788\"><\/ins><br \/>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<h1><strong>Solution 2: Union Find<\/strong><\/h1>\n<p>Find all connected components (islands)<\/p>\n<p>Ans = # of stones &#8211; # of islands<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua, running time: 16 ms\nclass Solution {\npublic:\n  int removeStones(vector&lt;vector&lt;int&gt;&gt;&amp; stones) {\n    const int kSize = 10000;\n    DSU dsu(kSize * 2);\n    for (const auto&amp; stone : stones)\n      dsu.Union(stone[0], stone[1] + kSize);\n    unordered_set&lt;int&gt; seen;\n    for (const auto&amp; stone : stones)\n      seen.insert(dsu.Find(stone[0]));\n    return stones.size() - seen.size();\n  }\nprivate:\n  class DSU {\n  public:\n    DSU(int n): p_(n) {\n      for (int i = 0 ; i &lt; n; ++i)\n        p_[i] = i;              \n    }\n    \n    void Union(int i, int j) {\n      p_[Find(i)] = Find(j);      \n    }\n    \n    int Find(int i) {\n      if (p_[i] != i) p_[i] = Find(p_[i]);\n      return p_[i];\n    }    \n  private:\n    vector&lt;int&gt; p_;    \n  };\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-200-number-of-islands\/\">\u82b1\u82b1\u9171 LeetCode 200. Number of Islands<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem On a 2D plane, we place stones at some integer coordinate points.&nbsp; Each coordinate point may have at most one stone. Now, a&nbsp;move&nbsp;consists 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":[76],"tags":[102,433,113],"class_list":["post-4379","post","type-post","status-publish","format-standard","hentry","category-graph","tag-connected-components","tag-island","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4379","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=4379"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4379\/revisions"}],"predecessor-version":[{"id":5641,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4379\/revisions\/5641"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}