LeetCode554(2021.5.2)

LC554. 砖墙

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
        unordered_map<int, int> cnt;
        for (auto widths : wall) {
            int n = widths.size(), sum = 0;
            for (int i = 0; i < n - 1; ++i) {
                sum += widths[i];
                cnt[sum]++;
            }
        }
        int maxCnt = 0;
        for (auto c : cnt) maxCnt = max(maxCnt, c.second);
        return wall.size() - maxCnt;
    }
};

发表评论