<p>There is a rectangular brick wall in front of you with <code>n</code> rows of bricks. The <code>i<sup>th</sup></code> row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.</p>

<p>Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. 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.</p>

<p>Given the 2D array <code>wall</code> that contains the information about the wall, return <em>the minimum number of crossed bricks after drawing such a vertical line</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg" style="width: 493px; height: 577px;" />
<pre>
<strong>Input:</strong> wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> wall = [[1],[1],[1]]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == wall.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= wall[i].length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= sum(wall[i].length) &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>sum(wall[i])</code> is the same for each row <code>i</code>.</li>
	<li><code>1 &lt;= wall[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
           //the gap between two bricks is so-called "cut-face"
   //if we can draw a vertical line alone the position which
   //has much "cut-face" ,then the brick we will cross will be lesser
   //i.e. In below example, if we can draw a vertical line alone
   //   the 2-th brick of 0 row, 1-th brick of 1 row and 2-th brick of 2-th row
   //   then no brick will be crossed
   // [1][2]|[1]
   // [ 3  ]|[1]
   // [2][1]|[1]
   //the the idea is to create a hash table to store <sum of bricks (0~n), the number of this sum>
   //each sum can be refered as "cut-face"
   //therefore, the numebr of a specific sum can be treated as the brick won't be crossed
   //so after hash table is established, we will find out the max number in hash table
   //the the ans=the number of row of wall - max number


   int n=wall.size(); //the total row of a given wall
   map<int,int> m;
   //int max_num =0; //used to store the current max number of "cut-face"
   //go through each row in a given wall
   for(auto row:wall)
   {
       int sum=0;
       int cnt = row.size();//the number of bricks in this row
       //due to the line along one of two vertical edges of the wall can't be counted
       //therefore, the accumulation of brick's length is up to (cnt-1)-th brick
       //for example, if the number of brick in this row is 3
       //then we can only accumulate the sum from 0 to 1 (sum0=row[0],sum1=row[0]+row[1])
       for(int i=0;i<cnt-1;i++)
       {
           sum+=row[i];
           //increase the number of this sum
           m[sum]++;
           //compare current number of "cut-face" with previous max one
           //the pick up the max one
           //max_num = max(max_num,m[sum]);
       }
   }


   //when goes here, the hash table has been estabished, let find out the max numebr in this
   //table
   int max_num = 0;
   for(auto it:m)
   {
      max_num = max(max_num,it.second);
   }


   //the minum number of crossed will be n-max_num
   return n-max_num;

    }
};
28000