-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 115: Largest Square area in Matrix
- Loading branch information
1 parent
f17aff8
commit de7370f
Showing
6 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
34 changes: 34 additions & 0 deletions
34
Lectures/Lecture_115/Lecture_Codes/largest_square_formed_in_a_matrix_1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
Lectures/Lecture_115/Lecture_Codes/largest_square_formed_in_a_matrix_2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
35 changes: 35 additions & 0 deletions
35
Lectures/Lecture_115/Lecture_Codes/largest_square_formed_in_a_matrix_3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
Lectures/Lecture_115/Lecture_Codes/largest_square_formed_in_a_matrix_4.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |