straightforward c++ solution, 39ms, beat 80%


  • 0
    S

    calculate the brick's x coordinate and put in a map. and then get the maximum count and subtract from the layers.

    class Solution {
    public:
        int leastBricks(vector<vector<int>>& wall) {
            //add each layer to get the x position and count the max one!
            unordered_map<int,int> mp;
            for(int i=0;i<wall.size();i++)
            {
                int x=0;
                for(int j=0;j<wall[i].size()-1;j++) //note the last one cannot be accounted
                {
                    x+=wall[i][j];
                    mp[x]++;
                }
            }
            //find the max
            int maxlen=0;
            for(auto it=mp.begin();it!=mp.end();it++) if(maxlen<it->second) maxlen=it->second;
            return wall.size()-maxlen;
            
            
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.