c++ code with Hash Map


  • 0
    G
    class Solution {
    public:
    
        int leastBricks(vector<vector<int>>& wall) 
        {
            unordered_map<int,int> Ends;
            
            for(auto &V : wall)
            {
                int Start = 0;
                
                for(int i = 0; i < V.size() - 1; ++i)
                {
                    Ends[Start + V[i]]++;
                    Start += V[i];
                }
            }
            
            int Min = INT_MAX;
            
            for(auto &P : Ends)
            {
                Min = min(Min,(int)wall.size() - P.second);
            }
            
            return (Min == INT_MAX ? wall.size() : Min);
        }
    };

Log in to reply
 

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