Brick Wall LeetCode solution in Java

1
class Solution {
2
public int leastBricks(List<List<Integer>> wall) {
3
HashMap<Integer, Integer> edge_frequency =
4
new HashMap<>(); // HashMap to store the number of common edges among the rows
5
int max_frequency = 0; // Variable to store the frequency of most occuring edge
6

7
for (int row = 0; row < wall.size(); row++) // Iterating through each row
8
{
9
int edge_postion = 0; // Variable to store different edge postion
10

11
for (int brick_no = 0;
12
brick_no < wall.get(row).size() - 1;
13
brick_no++) // Iterating through each brick inside a row
14
{
15
int current_brick_length = wall.get(row).get(brick_no); // Length of the current brick
16
edge_postion =
17
edge_postion
18
+ current_brick_length; // Next Edge Position = Previous Edge Position + Current
19
// Brick's Length
20
edge_frequency.put(
21
edge_postion,
22
edge_frequency.getOrDefault(edge_postion, 0)
23
+ 1); // Incrementing the Frequency of just calculated Edge Postion
24
max_frequency =
25
Math.max(
26
edge_frequency.get(edge_postion),
27
max_frequency); // Comparing the "Frequency of just calculated Edge Postion" with
28
// "Max Frequency seen till now" & storing whichever is greater.
29
}
30
}
31
return wall.size()
32
- max_frequency; // returning (Number of Bricks Crossed by Line) i.e. (Number of Rows in
33
// Wall - Frequency of Most Occuring Edge)
34
}
35
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0