程式語言 - LeetCode - C++ - 554. Brick Wall



題目:


解答:

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
        unordered_map<long, int> mp;

        for (auto& w : wall) {
            long len = 0;

            for (int i = 0; i < w.size() - 1; ++i) {
                len += w[i];
                mp[len] += 1;
            }
        }

        int ans = 0;

        for (auto& [k, v] : mp) {
            ans = max(ans, v);
        }

        return wall.size() - ans;
    }
};