hashmap C++ solution


  • 0
    I
        int leastBricks(vector<vector<int>>& wall) {
            int n = wall.size();
            int w = 0;
            for(int i = 0; i < wall[0].size();++i)
               w+=wall[0][i];
            unordered_map<int,int> m;
            for(int i = 0; i < n; ++i) {
                int now = wall[i][0];
                for(int j = 1; j < wall[i].size(); ++j) {
                    ++m[now];
                    now += wall[i][j];
                }
            }
            int ans = n;
            for(auto it = m.begin(); it != m.end(); ++it)
                if(it->first != 0 && it->first != w)
                   ans = min(ans,n-it->second);
            return ans;
        }
    

Log in to reply
 

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