Skip to content

Commit 554d8d2

Browse files
committed
day 22
1 parent 4a2a387 commit 554d8d2

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Brick Wall
3+
==========
4+
5+
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 top to the bottom and cross the least bricks.
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
Example:
14+
Input: [[1,2,2,1],
15+
[3,1,2],
16+
[1,3,2],
17+
[2,4],
18+
[3,1,2],
19+
[1,3,1,1]]
20+
Output: 2
21+
22+
Note:
23+
The width sum of bricks in different rows are the same and won't exceed INT_MAX.
24+
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't exceed 20,000.
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
int leastBricks(vector<vector<int>> &wall)
31+
{
32+
if (wall.size() == 1)
33+
{
34+
if (wall[0].size() == 1)
35+
return 1;
36+
return 0;
37+
}
38+
39+
if (wall[0].size() == 1 && wall[0][0] == 1)
40+
return wall.size();
41+
vector<vector<int>> dp;
42+
43+
for (auto &row : wall)
44+
{
45+
vector<int> arr = {row[0]};
46+
for (int i = 1; i < row.size(); ++i)
47+
{
48+
arr.push_back({arr[i - 1] + row[i]});
49+
}
50+
dp.push_back(arr);
51+
}
52+
53+
int ans = INT_MIN;
54+
unordered_map<int, int> freq;
55+
56+
for (auto &row : dp)
57+
{
58+
for (auto &i : row)
59+
{
60+
if (i != dp[0][dp[0].size() - 1])
61+
freq[i]++;
62+
ans = max(ans, freq[i]);
63+
}
64+
}
65+
return dp.size() - ans;
66+
}
67+
};

Leetcode Daily Challenge/April-2021/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| 16. | [Remove All Adjacent Duplicates in String II](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3710/) | [cpp](./16.%20Remove%20All%20Adjacent%20Duplicates%20in%20String%20II.cpp) |
2121
| 17. | [Number of Submatrices That Sum to Target](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3711/) | [cpp](./17.%20Number%20of%20Submatrices%20That%20Sum%20to%20Target.cpp) |
2222
| 18. | [Remove Nth Node From End of List](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3712/) | [cpp](./18.%20Remove%20Nth%20Node%20From%20End%20of%20List.cpp) |
23-
| 19. | [Combination Sum IV](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3713/) | [cpp](./19.%20Combination%20Sum%20IV.cpp) |
24-
| 20. | [N-ary Tree Preorder Traversal](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3714/) | [cpp](./20.%20N-ary%20Tree%20Preorder%20Traversal.cpp) |
25-
| 21. | [Triangle](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3715/) | [cpp](./21.%20Triangle.cpp) |
23+
| 19. | [Combination Sum IV](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3713/) | [cpp](./19.%20Combination%20Sum%20IV.cpp) |
24+
| 20. | [N-ary Tree Preorder Traversal](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3714/) | [cpp](./20.%20N-ary%20Tree%20Preorder%20Traversal.cpp) |
25+
| 21. | [Triangle](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/595/week-3-april-15th-april-21st/3715/) | [cpp](./21.%20Triangle.cpp) |
26+
| 22. | [Brick Wall](https://leetcode.com/explore/challenge/card/april-leetcoding-challenge-2021/596/week-4-april-22nd-april-28th/3717/) | [cpp](./22.%20Brick%20Wall.cpp) |

0 commit comments

Comments
 (0)