首页 > 其他 > 详细

【每日一题】554. 砖墙

时间:2021-05-02 22:04:22      阅读:43      评论:0      收藏:0      [点我收藏+]

https://leetcode-cn.com/problems/brick-wall/

技术分享图片

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        Map<Integer, Integer> pos2LeftAndCount = new HashMap<>();
        for(List<Integer> w : wall){
            int len = 0;
            for(int i = 0; i < w.size() - 1; i++){
                len += w.get(i);
                pos2LeftAndCount.put(len, pos2LeftAndCount.getOrDefault(len, 0) + 1);
            }
        }
        Integer num = 0;
        for(Map.Entry entry : pos2LeftAndCount.entrySet()){
            num = Math.max((Integer) entry.getValue(), num);
        }
        return wall.size() - num;
    }
}

【每日一题】554. 砖墙

原文:https://www.cnblogs.com/realzhaijiayu/p/14726003.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!