原题
https://leetcode-cn.com/problems/brick-wall/
思路
哈希
题解
package cn.codemao.botmao.admin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @version 1.0
* @author: zhangkun@codemao.cn
* @description:
* @create: 2021-05-02 23:34
**/
public class Code554 {
public static void main(String[] args) {
}
public static int leastBricks(List<List<Integer>> wall) {
// 有多少行
int row = wall.size();
// 每块转叠加的长度作为key, 对应长度出现的次数作为value
Map<Integer, Integer> map = new HashMap<>();
int max = 0;
for (List<Integer> tempRow : wall) {
int width = 0;
for (int i = 0; i < tempRow.size()-1; i++) {
width += tempRow.get(i);
int value = map.getOrDefault(width, 0)+1;
map.put(width, value);
if (max <= value) {
max = value;
}
}
}
return row - max;
}
}