Leetcode 554. 砖墙

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {

        unordered_map<int,int> mp;//统计缝的重复数量
        for(int i=0;i<wall.size();++i){
            int len=0;
            for(int j=0;j<wall[i].size()-1;++j){//注意不统计最后一个位置
                len+=wall[i][j];//该层的各个长度
                ++mp[len];
            }
        }
        int res=0;
        for(auto& it:mp){//找出最大值
            res=max(res,it.second);
        }
        return wall.size()-res;//返回最多的穿过的缝隙


    }
};

----逆向思维

原文地址:https://www.cnblogs.com/cunyusup/p/14246162.html