C++ 43ms greedy solution :)


  • 0
    F
    class Solution {
    public:
        int leastBricks(vector<vector<int>>& wall) 
        {
            if (wall.empty())
                return -1;
            
            unordered_map<int, int> hash;
            
            for (int i = 0; i < wall.size(); ++i)
            {
                if (wall[i].empty())
                    return -1;
                
                for (int j = 0; j < wall[i].size(); ++j)
                {
                    if (j > 0)
                        wall[i][j] += wall[i][j - 1];
    
                    ++hash[wall[i][j]];
                }
            }
    
            int max_len = wall[0][wall[0].size() - 1];
            
            int max = INT_MIN;
            for(auto &item: hash)
            {
                max = (max < item.second && item.first != max_len) ? item.second : max;
            }
            
            return max == INT_MIN ? wall.size() : wall.size() - max;
        }
    };
    

Log in to reply
 

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