C++ Unordered_map, Beats 100%


  • 0
    A

    at each level, lets measure complete length so far of row after addition of each brick from the left. As last brick addition is useless for us, we will not consider its length. Now we will use map to store the count of length measured among all rows. and then find the length with maximum count. Our answer will be difference of row count and maximum count.

    public:
        int leastBricks(vector<vector<int>>& wall) {
            unordered_map<int,int>hash;
            
            for(int i=0; i<wall.size();i++)
            {
                int temp=0;
                for(int j=0; j<wall[i].size()-1;j++)
                {
                    temp+=wall[i][j];
                    hash[temp]++;
                }
                temp=0;
            }
            int max=0;
            for(auto it=hash.begin(); it!=hash.end();it++)
            {
                if(it->second>max)
                    max=it->second;
            }
            return wall.size()-max;
            
        }
    };

Log in to reply
 

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