LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

题目

我是按照边进行二分的

class Solution {
public:
    int sum[100005];
    
    int a[305][305];
    int maxSideLength(vector>& mat, int threshold) {
        
        if(threshold==0)
            return 0;
        int n = mat.size(); 
        int m = mat[0].size();
        
        int len = min(n,m);
        
        for(int i=0;i<=len;i++)
        {
            sum[i]=99999999;
        }
        
        a[0][0] = mat[0][0];
        for(int j=1;j=0)
                        {
                            x = x-a[i-k-1][j];
                        }
                        if(j-k-1>=0)
                        {
                            x = x-a[i][j-k-1];
                        }
                        if(i-k-1>=0&&j-k-1>=0)
                        {
        
                            x = x+a[i-k-1][j-k-1];
                        }
                       
                    }
                    
                    if(x<=threshold)
                    {
                        sum[k+1] = min(sum[k+1],x);
                    }
                }
            }
        }
        
        int start = 1;
        int end = len;
        
        int ans=-1;
        
        while(start<=end)
        {
            int mid = (start + end)/2;
            
            if(sum[mid]>threshold)
            {
                end = mid-1;
            }
            
            if(sum[mid]threshold)
                ans=0;
            else
                ans=end;
        }
        return ans;
        
    }
};

你可能感兴趣的:(LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold)