
g0501_0600.s0554_brick_wall.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0554_brick_wall;
// #Medium #Array #Hash_Table #2022_03_20_Time_13_ms_(75.64%)_Space_56.2_MB_(5.86%)
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public int leastBricks(List> wall) {
Map map = new HashMap<>();
for (List row : wall) {
int sum = 0;
for (int i = 0; i < row.size() - 1; i++) {
sum += row.get(i);
if (map.containsKey(sum)) {
map.put(sum, map.get(sum) + 1);
} else {
map.put(sum, 1);
}
}
}
int result = wall.size();
for (Map.Entry entry : map.entrySet()) {
result = Math.min(result, wall.size() - entry.getValue());
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy