{"id":430,"date":"2021-04-06T21:36:47","date_gmt":"2021-04-06T21:36:47","guid":{"rendered":"https:\/\/uqcodewall.com\/?page_id=430"},"modified":"2021-05-29T20:38:57","modified_gmt":"2021-05-29T20:38:57","slug":"947-most-stones-removed-with-same-row-or-column","status":"publish","type":"page","link":"https:\/\/uqcodewall.com\/?page_id=430","title":{"rendered":"947. Most Stones Removed With Same Row Or Column"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\/**<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 947. Most Stones Removed with Same Row or Column<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* return the largest possible number of stones that can be removed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* Example 1:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Output: 5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Explanation: One way to remove 5 stones is as follows:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 1. Remove stone [2,2] because it shares the same row as [2,1].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 2. Remove stone [2,1] because it shares the same column as [0,1].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 3. Remove stone [1,2] because it shares the same row as [1,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 4. Remove stone [1,0] because it shares the same column as [0,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 5. Remove stone [0,1] because it shares the same row as [0,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Stone [0,0] cannot be removed since it does not share a row\/column with another stone still on the plane.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* Example 2:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Output: 3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Explanation: One way to make 3 moves is as follows:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 1. Remove stone [2,2] because it shares the same row as [2,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 2. Remove stone [2,0] because it shares the same column as [0,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* 3. Remove stone [0,2] because it shares the same row as [0,0].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Stones [0,0] and [1,1] cannot be removed since they do not share a row\/column with another stone still on the plane.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u00a0* Example 3:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Input: stones = [[0,0]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Output: 0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* Explanation: [0,0] is the only stone on the plane, so you cannot remove it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">&nbsp;* \/<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See the solution on github here:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/zcoderz\/leetcode\/blob\/main\/src\/main\/java\/graph\/StonesRemoved.java\">https:\/\/github.com\/zcoderz\/leetcode\/blob\/main\/src\/main\/java\/graph\/StonesRemoved.java<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution will use union find (disjoint sets) to solve the problem. See the union find algorithm on wiki pedia here: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Disjoint-set_data_structure\">https:\/\/en.wikipedia.org\/wiki\/Disjoint-set_data_structure<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See my code on union find at github here: <a href=\"https:\/\/github.com\/zcoderz\/leetcode\/blob\/main\/src\/main\/java\/utils\/graph\/generic\/UnionFindNode.java\">https:\/\/github.com\/zcoderz\/leetcode\/blob\/main\/src\/main\/java\/utils\/graph\/generic\/UnionFindNode.java<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The algorithm is:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Iterate through the stones while connecting the stones on the same row and those on the same column via union\/find \u2013 union operation<ol><li>We add (2000) to the column value so as to make them distinct from the row value<\/li><\/ol><\/li><li>We iterate through the stones via creating a unique parent set via calling the find method on each union find node.<\/li><li>The number of parent nodes returned in 2 identify the unique groups of connected stones.<\/li><li>We can calculate the stones removed via subtracting total stones from the # in 3 (stones left).<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Here is the code:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic int removeStones(int&#x5B;]&#x5B;] stones) {\n    Map&lt;Integer, UnionFindNode&lt;Integer&gt;&gt; unionFindMap = new HashMap&lt;&gt;();\n    for (int i = 0; i &lt; stones.length; i++) {\n        UnionFindNode&lt;Integer&gt; unionA = new UnionFindNode&lt;&gt;(stones&#x5B;i]&#x5B;0]);\n        \/\/adding 20000 to cols , so columns and rows have diff ids\n        UnionFindNode&lt;Integer&gt; unionB = new UnionFindNode&lt;&gt;(stones&#x5B;i]&#x5B;1] + 20000);\n        unionFindMap.put(stones&#x5B;i]&#x5B;0], unionA);\n        unionFindMap.put(stones&#x5B;i]&#x5B;1] + 20000, unionB);\n        unionA.union(unionB);\n    }\n    Set&lt;Integer&gt; parentsSet = new HashSet&lt;&gt;();\n    for (int i = 0; i &lt; stones.length; i++) {\n        UnionFindNode&lt;Integer&gt; unionA = unionFindMap.get(stones&#x5B;i]&#x5B;0]);\n        parentsSet.add(unionA.find().getValue());\n    }\n    return stones.length - parentsSet.size();\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\/** &nbsp;* 947. Most Stones Removed with Same Row or Column \u00a0* On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone. \u00a0* A stone can be removed if it shares either the same row or the same column as another stone that<a class=\"more-link\" href=\"https:\/\/uqcodewall.com\/?page_id=430\">Continue reading <span class=\"screen-reader-text\">&#8220;947. Most Stones Removed With Same Row Or Column&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"wf_page_folders":[21],"class_list":["post-430","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/pages\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uqcodewall.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=430"}],"version-history":[{"count":5,"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/pages\/430\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/uqcodewall.com\/index.php?rest_route=\/wp\/v2\/pages\/430\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/uqcodewall.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430"}],"wp:term":[{"taxonomy":"wf_page_folders","embeddable":true,"href":"https:\/\/uqcodewall.com\/index.php?rest_route=%2Fwp%2Fv2%2Fwf_page_folders&post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}