Skip to content

Commit

Permalink
Lecture 115: Largest Square area in Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanrajput23 authored Oct 16, 2024
1 parent f17aff8 commit de7370f
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
Binary file added Lectures/Lecture_115/Lecture_115_Hands_On.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions Lectures/Lecture_115/Lecture_Codes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Lecture Codes

| **Problem** | **Solution** | **Level** |
|:--------------|:--------------:|:-----------:|
| [Largest square formed in a matrix](https://www.geeksforgeeks.org/problems/largest-square-formed-in-a-matrix0806/1) | [Solution1](), [Solution2](), [Solution3](), [Solution4]() | Medium |
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Gives TLE

class Solution {
public:
int solve(vector<vector<int>> &mat, int i, int j, int &maxi, int &n, int &m) {
if (i >= n || j >= m) {
return 0;
}

int right = solve(mat, i, j+1, maxi, n, m);
int diagonal = solve(mat, i+1, j+1, maxi, n, m);
int down = solve(mat, i+1, j, maxi, n, m);

if (mat[i][j] == 1) {
int ans = 1 + min(right, min(diagonal, down));

maxi = max(maxi, ans);

return ans;
}
else {
return 0;
}
}

int maxSquare(int n, int m, vector<vector<int>> mat) {
// code here
int maxi = 0;

solve(mat, 0, 0, maxi, n, m);

return maxi;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Recursion + Memoization

class Solution {
public:
int solve(vector<vector<int>> &mat, int i, int j, int &maxi, int &n, int &m, vector<vector<int>> &dp) {
if (i >= n || j >= m) {
return 0;
}

if (dp[i][j] != -1) {
return dp[i][j];
}

int right = solve(mat, i, j+1, maxi, n, m, dp);
int diagonal = solve(mat, i+1, j+1, maxi, n, m, dp);
int down = solve(mat, i+1, j, maxi, n, m, dp);

if (mat[i][j] == 1) {
dp[i][j] = 1 + min(right, min(diagonal, down));

maxi = max(maxi, dp[i][j]);

return dp[i][j];
}
else {
return dp[i][j] = 0;
}
}

int maxSquare(int n, int m, vector<vector<int>> mat) {
// code here
vector<vector<int>> dp(n, vector<int>(m ,-1));
int maxi = 0;

solve(mat, 0, 0, maxi, n, m, dp);

return maxi;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// tabulation method

class Solution {
public:
int solve(vector<vector<int>> &mat, int &maxi, int &n, int &m) {

vector<vector<int>> dp(n+1, vector<int>(m+1 ,0));

for (int i=n-1; i>=0; i--) {
for (int j=m-1; j>=0; j--) {
int right = dp[i][j+1];
int diagonal = dp[i+1][j+1];
int down = dp[i+1][j];

if (mat[i][j] == 1) {
dp[i][j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, dp[i][j]);
}
else {
dp[i][j] = 0;
}
}
}
return dp[0][0];
}

int maxSquare(int n, int m, vector<vector<int>> mat) {
// code here
int maxi = 0;

solve(mat, maxi, n, m);

return maxi;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// space optimization

class Solution {
public:
int solve(vector<vector<int>> &mat, int &maxi, int &n, int &m) {
vector<int> curr(m+1, 0);
vector<int> next(m+1, 0);

for (int i=n-1; i>=0; i--) {
for (int j=m-1; j>=0; j--) {
int right = curr[j+1];
int diagonal = next[j+1];
int down = next[j];

if (mat[i][j] == 1) {
curr[j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, curr[j]);
}
else {
curr[j] = 0;
}
}
next = curr;
}
return next[0];
}

int maxSquare(int n, int m, vector<vector<int>> mat) {
// code here
int maxi = 0;

solve(mat, maxi, n, m);

return maxi;
}
};

0 comments on commit de7370f

Please sign in to comment.