用map统计边缘即可
最后求出哪个边缘出现次数cnt最多
返回n - cnt即可
class Solution { public: map<int, int> vis; set<int> s; int leastBricks(vector<vector<int>>& wall) { int n = wall.size(), m, sum; int ans = 0; for(int i = 0; i < n; i++) { sum = 0; m = wall[i].size(); for(int j = 0; j < m - 1; j++) { sum += wall[i][j]; if(vis.count(sum) == 0) vis[sum] = 0; vis[sum]++; } } int cnt = 0; for(int i = 0; i < n; i++) { sum = 0; m = wall[i].size(); for(int j = 0; j < m - 1; j++) { sum += wall[i][j]; cnt = max(cnt, vis[sum]); } } return n - cnt; } };
原文:https://www.cnblogs.com/WTSRUVF/p/14725952.html