{"id":2294,"date":"2018-03-22T09:07:54","date_gmt":"2018-03-22T16:07:54","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2294"},"modified":"2018-03-22T09:08:02","modified_gmt":"2018-03-22T16:08:02","slug":"leetcode-554-brick-wall","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-554-brick-wall\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 554. Brick Wall"},"content":{"rendered":"<div class=\"question-description\">\n<h1><strong>Problem<\/strong><\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/brick-wall\/description\/\">https:\/\/leetcode.com\/problems\/brick-wall\/description\/<\/a><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u4ece\u5c0f\u5230\u4e0b\u5207\u4e00\u5200\uff0c\u6700\u5c11\u4f1a\u5207\u5230\u591a\u5c11\u5757\u7816\u3002<\/p>\n<p>There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the\u00a0<b>top<\/b>\u00a0to the\u00a0<b>bottom<\/b>\u00a0and cross the\u00a0<b>least\u00a0<\/b>bricks.<\/p>\n<p>The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.<\/p>\n<p>If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.<\/p>\n<p><b>You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.<\/b><\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> \r\n[[1,2,2,1],\r\n [3,1,2],\r\n [1,3,2],\r\n [2,4],\r\n [3,1,2],\r\n [1,3,1,1]]\r\n<b>Output:<\/b> 2\r\n<b>Explanation:<\/b> \r\n<img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/brick_wall.png\" width=\"30%\" \/>\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The width sum of bricks in different rows are the same and won&#8217;t exceed INT_MAX.<\/li>\n<li>The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won&#8217;t exceed 20,000.<\/li>\n<\/ol>\n<\/div>\n<h1>Solution: HashTable<\/h1>\n<p>Count boundaries, cut at the location with most common boundaries.<\/p>\n<p>Time complexity: O(|bricks|)<\/p>\n<p>Space complexity: O(|bricks|)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 42 ms\r\nclass Solution {\r\npublic:\r\n  int leastBricks(vector&lt;vector&lt;int&gt;&gt;&amp; wall) {\r\n    unordered_map&lt;int, int&gt; count;\r\n    int max_count = 0;\r\n    for (const auto&amp; bricks : wall) {\r\n      int s = 0;\r\n      for (int i = 0; i &lt; bricks.size() - 1; ++i) {\r\n        s += bricks[i];\r\n        max_count = max(max_count, ++count[s]);\r\n      }\r\n    }\r\n    return wall.size() - max_count;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/brick-wall\/description\/ \u9898\u76ee\u5927\u610f\uff1a\u4ece\u5c0f\u5230\u4e0b\u5207\u4e00\u5200\uff0c\u6700\u5c11\u4f1a\u5207\u5230\u591a\u5c11\u5757\u7816\u3002 There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177],"class_list":["post-2294","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2294","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=2294"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2294\/revisions"}],"predecessor-version":[{"id":2296,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2294\/revisions\/2296"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}