C++ Solution


  • 0
    C
    
    using namespace std;
    
    class Solution {
    
    public:
    
    	int n,m;
    	unordered_set<int> st;
    	vector<int> nums;
    	int cnt[10100];
    
        int leastBricks(vector<vector<int>>& wall) 
    	{
    		n = wall.size();
    		for(int i=0;i<n;i++)
    		{
    			m = wall[i].size();
    			int base = 0;
    			for(int j=0;j<m;j++)
    			{
    				int x = wall[i][j];
    				base += x;
    				st.insert(base);
    			}
    		}
    
    		unordered_set<int>::iterator it;
    		for(it = st.begin();it!=st.end();it++) nums.push_back(*it);
    		sort(nums.begin(),nums.end());
    
    		for(int i=0;i<n;i++)
    		{
    			m = wall[i].size();
    			int base = 0;
    			for(int j=0;j<m;j++)
    			{
    				int x = wall[i][j];
    				base += x;
    				int pos = lower_bound(nums.begin(),nums.end(),base) - nums.begin();
    				cnt[pos]++;
    			}
    		}
    
    		int ret = n;
    
    		for(int i=0,sz=st.size();i<sz-1;i++)
    		{
    			ret = min(ret,n-cnt[i]);
    		}
    
    		return ret;
        }
    
    };
    

Log in to reply
 

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