Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maximum rectangle sum #1826

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Program's_Contributed_By_Contributors/rectangle sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//saurabh kumar
//https://github.com/saurabhkumar1432
//date-1 october 2022
//time - 00:26
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
int m=matrix.size(),n=matrix[0].size();
vector<vector<int>>dp(m+1,vector<int>(n,0));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
dp[i+1][j]=dp[i][j]+matrix[i][j];
}
}
int ans=INT_MIN;
for(int i=0;i<m;i++){
for(int l=i+1;l<=m;l++){
for(int j=0;j<n;j++){
int val=0;
for(int k=j;k<n;k++){
val+=dp[l][k]-dp[i][k];

if(val<k){
ans=max(ans,val);
}
else if(val==k){
return k;
}
}
}
}
}
return ans;

}
};
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
int m=matrix.size(),n=matrix[0].size();
vector<vector<int>>dp(m+1,vector<int>(n,0));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
dp[i+1][j]=dp[i][j]+matrix[i][j];
}
}
int ans=INT_MIN;
for(int i=0;i<m;i++){
for(int l=i+1;l<=m;l++){
for(int j=0;j<n;j++){
int val=0;
for(int k=j;k<n;k++){
val+=dp[l][k]-dp[i][k];

if(val<k){
ans=max(ans,val);
}
else if(val==k){
return k;
}
}
}
}
}
return ans;

}
};