From b1cb87aae6d72288ca66190bb0affa0726d9c027 Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 18:43:46 +0530 Subject: [PATCH 01/11] maximalRectangle --- .../Maximal Rectangle/program.c | 71 +++++++++++++++++++ .../Maximal Rectangle/readme.in | 35 +++++++++ .../Swim in Rising Water/program.c | 3 +- 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Dynamic Programming/Maximal Rectangle/program.c create mode 100644 Dynamic Programming/Maximal Rectangle/readme.in diff --git a/Dynamic Programming/Maximal Rectangle/program.c b/Dynamic Programming/Maximal Rectangle/program.c new file mode 100644 index 00000000..ea377d5f --- /dev/null +++ b/Dynamic Programming/Maximal Rectangle/program.c @@ -0,0 +1,71 @@ +#include +#include +#include + +int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize) { + if (matrix == NULL || matrixSize == 0 || matrixColSize[0] == 0) { + return 0; + } + + int m = matrixSize; + int n = matrixColSize[0]; + + int* heights = (int*)calloc(n, sizeof(int)); + int* leftBoundaries = (int*)calloc(n, sizeof(int)); + int* rightBoundaries = (int*)malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) { + rightBoundaries[i] = n; + } + + int maxRectangle = 0; + + for (int i = 0; i < m; i++) { + int left = 0; + int right = n; + + updateHeightsAndLeftBoundaries(matrix[i], heights, leftBoundaries, n, &left); + + updateRightBoundaries(matrix[i], rightBoundaries, n, &right); + + maxRectangle = calculateMaxRectangle(heights, leftBoundaries, rightBoundaries, n, maxRectangle); + } + + free(heights); + free(leftBoundaries); + free(rightBoundaries); + + return maxRectangle; +} + +void updateHeightsAndLeftBoundaries(char* row, int* heights, int* leftBoundaries, int n, int* left) { + for (int j = 0; j < n; j++) { + if (row[j] == '1') { + heights[j]++; + leftBoundaries[j] = leftBoundaries[j] > *left ? leftBoundaries[j] : *left; + } else { + heights[j] = 0; + leftBoundaries[j] = 0; + *left = j + 1; + } + } +} + +void updateRightBoundaries(char* row, int* rightBoundaries, int n, int* right) { + for (int j = n - 1; j >= 0; j--) { + if (row[j] == '1') { + rightBoundaries[j] = rightBoundaries[j] < *right ? rightBoundaries[j] : *right; + } else { + rightBoundaries[j] = n; + *right = j; + } + } +} + +int calculateMaxRectangle(int* heights, int* leftBoundaries, int* rightBoundaries, int n, int maxRectangle) { + for (int j = 0; j < n; j++) { + int width = rightBoundaries[j] - leftBoundaries[j]; + int area = heights[j] * width; + maxRectangle = maxRectangle > area ? maxRectangle : area; + } + return maxRectangle; +} diff --git a/Dynamic Programming/Maximal Rectangle/readme.in b/Dynamic Programming/Maximal Rectangle/readme.in new file mode 100644 index 00000000..a873d999 --- /dev/null +++ b/Dynamic Programming/Maximal Rectangle/readme.in @@ -0,0 +1,35 @@ +Problem Overview +The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. + +Approach +The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. + +Steps to Solve +Initialize Arrays: + +heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. +leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. +rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. +Row-by-Row Processing: + +For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. +Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. +Calculate Maximum Rectangle Area: + +For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). +Update maxRectangle with the maximum area encountered. +Helper Functions +updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. + +updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. + +calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. + +Complexity Analysis +Time Complexity: +𝑂(𝑚×𝑛)O(m×n), where 𝑚 +m is the number of rows and +𝑛n is the number of columns. Each cell is processed once, and boundary updates are linear. +Space Complexity: 𝑂(𝑛) O(n), for storing heights, leftBoundaries, and rightBoundaries. +Summary +This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file diff --git a/Graph Algorithms/Swim in Rising Water/program.c b/Graph Algorithms/Swim in Rising Water/program.c index 756553db..ed7c86c7 100644 --- a/Graph Algorithms/Swim in Rising Water/program.c +++ b/Graph Algorithms/Swim in Rising Water/program.c @@ -25,7 +25,8 @@ bool helper(int x) { int r = queue[front][0]; int c = queue[front++][1]; - if (r == n - 1 && c == n - 1) { + if (r == n - 1 && c == n - 1) + { for (int i = 0; i < n; i++) { free(visited[i]); } From 749cd02b997eb05ee2334beff95abbf982981a6e Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 18:55:22 +0530 Subject: [PATCH 02/11] maximalRectangle --- .../Maximal Rectangle/README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Dynamic Programming/Maximal Rectangle/README.md diff --git a/Dynamic Programming/Maximal Rectangle/README.md b/Dynamic Programming/Maximal Rectangle/README.md new file mode 100644 index 00000000..a873d999 --- /dev/null +++ b/Dynamic Programming/Maximal Rectangle/README.md @@ -0,0 +1,35 @@ +Problem Overview +The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. + +Approach +The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. + +Steps to Solve +Initialize Arrays: + +heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. +leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. +rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. +Row-by-Row Processing: + +For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. +Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. +Calculate Maximum Rectangle Area: + +For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). +Update maxRectangle with the maximum area encountered. +Helper Functions +updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. + +updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. + +calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. + +Complexity Analysis +Time Complexity: +𝑂(𝑚×𝑛)O(m×n), where 𝑚 +m is the number of rows and +𝑛n is the number of columns. Each cell is processed once, and boundary updates are linear. +Space Complexity: 𝑂(𝑛) O(n), for storing heights, leftBoundaries, and rightBoundaries. +Summary +This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file From ec6caf8a865bafdf4e0dfa47e590259b78c872c2 Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 18:57:34 +0530 Subject: [PATCH 03/11] Maximal Rectangle --- .../Maximal Rectangle/readme.in | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 Dynamic Programming/Maximal Rectangle/readme.in diff --git a/Dynamic Programming/Maximal Rectangle/readme.in b/Dynamic Programming/Maximal Rectangle/readme.in deleted file mode 100644 index a873d999..00000000 --- a/Dynamic Programming/Maximal Rectangle/readme.in +++ /dev/null @@ -1,35 +0,0 @@ -Problem Overview -The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. - -Approach -The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. - -Steps to Solve -Initialize Arrays: - -heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. -leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. -rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. -Row-by-Row Processing: - -For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. -Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. -Calculate Maximum Rectangle Area: - -For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). -Update maxRectangle with the maximum area encountered. -Helper Functions -updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. - -updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. - -calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. - -Complexity Analysis -Time Complexity: -𝑂(𝑚×𝑛)O(m×n), where 𝑚 -m is the number of rows and -𝑛n is the number of columns. Each cell is processed once, and boundary updates are linear. -Space Complexity: 𝑂(𝑛) O(n), for storing heights, leftBoundaries, and rightBoundaries. -Summary -This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file From d8e14b5884803f6ce3a0d43f71cbac8c284fe773 Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 19:10:03 +0530 Subject: [PATCH 04/11] 85. Maximal Rectangle --- .../Maximal Rectangle/README.md | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Dynamic Programming/Maximal Rectangle/README.md b/Dynamic Programming/Maximal Rectangle/README.md index a873d999..5dbe47b5 100644 --- a/Dynamic Programming/Maximal Rectangle/README.md +++ b/Dynamic Programming/Maximal Rectangle/README.md @@ -27,9 +27,28 @@ calculateMaxRectangle: Iterates over each column and calculates the area for eac Complexity Analysis Time Complexity: -𝑂(𝑚×𝑛)O(m×n), where 𝑚 +𝑂 +( +𝑚 +× +𝑛 +) +O(m×n), where +𝑚 m is the number of rows and -𝑛n is the number of columns. Each cell is processed once, and boundary updates are linear. -Space Complexity: 𝑂(𝑛) O(n), for storing heights, leftBoundaries, and rightBoundaries. +𝑛 +n is the number of columns. Each cell is processed once, and boundary updates are linear. +Space Complexity: +𝑂 +( +𝑛 +) +O(n), for storing heights, leftBoundaries, and rightBoundaries. Summary -This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file +This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. + + + + + + From 992b5330b455b7f71d252efa28444dade6551dab Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 19:16:27 +0530 Subject: [PATCH 05/11] maximalRectangle --- Dynamic Programming/Maximal Rectangle/readme | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Dynamic Programming/Maximal Rectangle/readme diff --git a/Dynamic Programming/Maximal Rectangle/readme b/Dynamic Programming/Maximal Rectangle/readme new file mode 100644 index 00000000..5dbe47b5 --- /dev/null +++ b/Dynamic Programming/Maximal Rectangle/readme @@ -0,0 +1,54 @@ +Problem Overview +The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. + +Approach +The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. + +Steps to Solve +Initialize Arrays: + +heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. +leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. +rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. +Row-by-Row Processing: + +For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. +Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. +Calculate Maximum Rectangle Area: + +For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). +Update maxRectangle with the maximum area encountered. +Helper Functions +updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. + +updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. + +calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. + +Complexity Analysis +Time Complexity: +𝑂 +( +𝑚 +× +𝑛 +) +O(m×n), where +𝑚 +m is the number of rows and +𝑛 +n is the number of columns. Each cell is processed once, and boundary updates are linear. +Space Complexity: +𝑂 +( +𝑛 +) +O(n), for storing heights, leftBoundaries, and rightBoundaries. +Summary +This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. + + + + + + From 83685c91847681a84310d948363c05eb44bba6ab Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 19:20:32 +0530 Subject: [PATCH 06/11] maximalRectangle --- .../Minimum Cost to Cut a Stick/program.c | 50 --------------- .../Minimum Cost to Cut a Stick/readme | 64 ------------------- 2 files changed, 114 deletions(-) delete mode 100644 Dynamic Programming/Minimum Cost to Cut a Stick/program.c delete mode 100644 Dynamic Programming/Minimum Cost to Cut a Stick/readme diff --git a/Dynamic Programming/Minimum Cost to Cut a Stick/program.c b/Dynamic Programming/Minimum Cost to Cut a Stick/program.c deleted file mode 100644 index 8fd8d8eb..00000000 --- a/Dynamic Programming/Minimum Cost to Cut a Stick/program.c +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include - -// Comparison function for qsort (used to sort the cuts array) -int compare(const void *a, const void *b) { - return (*(int*)a - *(int*)b); -} - -int minCost(int n, int* cuts, int cutsSize) { - int* c = (int*)malloc((cutsSize + 2) * sizeof(int)); - for (int i = 0; i < cutsSize; i++) { - c[i + 1] = cuts[i]; - } - c[0] = 0; - c[cutsSize + 1] = n; - qsort(c, cutsSize + 2, sizeof(int), compare); - - int dp[cutsSize + 2][cutsSize + 2]; - for (int i = 0; i < cutsSize + 2; i++) { - for (int j = 0; j < cutsSize + 2; j++) { - dp[i][j] = 0; - } - } - - for (int i = 2; i < cutsSize + 2; i++) { - for (int j = i - 2; j >= 0; j--) { - dp[j][i] = INT_MAX; - for (int k = j + 1; k < i; k++) { - int cost = c[i] - c[j] + dp[j][k] + dp[k][i]; - if (cost < dp[j][i]) { - dp[j][i] = cost; - } - } - } - } - - int result = dp[0][cutsSize + 1]; - free(c); - return result; -} - -int main() { - int cuts[] = {1, 3, 4, 5}; - int n = 7; - int cutsSize = sizeof(cuts) / sizeof(cuts[0]); - int result = minCost(n, cuts, cutsSize); - printf("Minimum cost to cut the stick is %d\n", result); - return 0; -} diff --git a/Dynamic Programming/Minimum Cost to Cut a Stick/readme b/Dynamic Programming/Minimum Cost to Cut a Stick/readme deleted file mode 100644 index 368ac9ac..00000000 --- a/Dynamic Programming/Minimum Cost to Cut a Stick/readme +++ /dev/null @@ -1,64 +0,0 @@ -# Minimum Cost to Cut a Stick - -This project is an implementation of an algorithm to calculate the minimum cost required to cut a stick into specified pieces. The goal is to determine the optimal sequence of cuts that minimizes the total cost. - -## Problem Description - -Given a stick of length `n` and an array of integers `cuts`, where each integer represents a position on the stick where a cut can be made, the objective is to minimize the total cost of making the cuts. The cost of each cut is the length of the stick being cut at that step. The task is to compute the minimum cost to perform all cuts. - -### Example - -For a stick of length `n = 7` and cuts at positions `[1, 3, 4, 5]`, the minimum cost to cut the stick is computed by choosing the sequence of cuts that result in the lowest possible total cost. - -## Approach - -This solution uses dynamic programming to solve the problem efficiently. - -1. **Sorting the Cuts**: The positions of the cuts are sorted to determine the order of making cuts from the smallest to the largest position. - -2. **Dynamic Programming Table**: A 2D DP table (`dp`) is created, where `dp[i][j]` represents the minimum cost to cut the stick between positions `i` and `j`. - -3. **Iterative Solution**: Using a nested loop structure, the algorithm iteratively fills the DP table with minimum costs by checking possible partitions (cuts) between each sub-stick segment. - -4. **Result Extraction**: The final result, the minimum cost to cut the stick, is stored in `dp[0][cutsSize + 1]`, which represents the cost to cut the stick from the start to the end position. - -## Code Explanation - -The core code is implemented in C. Here is an overview of the key components: - -### 1. `compare` Function -A helper function used to sort the `cuts` array for correct order of cuts. - -### 2. `minCost` Function -This function calculates the minimum cost to cut the stick using dynamic programming. - -- **Parameters**: - - `n`: Length of the stick. - - `cuts`: Array of positions where cuts are needed. - - `cutsSize`: Number of cuts in the `cuts` array. - -- **Steps**: - 1. Initialize an array `c` to store sorted positions of cuts, including the start (0) and end (n) of the stick. - 2. Sort `c` for easier calculation. - 3. Define a 2D DP array and initialize all entries to zero. - 4. Iterate over possible sub-stick segments and compute the minimum cost for each segment using dynamic programming. - 5. Return the minimum cost from the `dp` array. - -### 3. `main` Function -The `main` function provides a test case to run the `minCost` function and displays the result. - -### Example Usage -The `main` function in this code provides an example usage of the `minCost` function, printing the minimum cost to cut the stick of length `7` with cuts at positions `[1, 3, 4, 5]`. - -### Input -The program can be modified to take different inputs for `n` and the `cuts` array. - -### Output -The output will display the minimum cost to make all cuts. - -## Installation - -1. Clone this repository. - ```bash - git clone https://github.com/yourusername/min-cost-stick-cutting.git - cd min-cost-stick-cutting From f2b393e71a021f333100e287371340e37c416f4d Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 19:24:20 +0530 Subject: [PATCH 07/11] maximalRectangle --- Dynamic Programming/Maximal Rectangle/readme | 54 -------------------- 1 file changed, 54 deletions(-) delete mode 100644 Dynamic Programming/Maximal Rectangle/readme diff --git a/Dynamic Programming/Maximal Rectangle/readme b/Dynamic Programming/Maximal Rectangle/readme deleted file mode 100644 index 5dbe47b5..00000000 --- a/Dynamic Programming/Maximal Rectangle/readme +++ /dev/null @@ -1,54 +0,0 @@ -Problem Overview -The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. - -Approach -The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. - -Steps to Solve -Initialize Arrays: - -heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. -leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. -rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. -Row-by-Row Processing: - -For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. -Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. -Calculate Maximum Rectangle Area: - -For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). -Update maxRectangle with the maximum area encountered. -Helper Functions -updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. - -updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. - -calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. - -Complexity Analysis -Time Complexity: -𝑂 -( -𝑚 -× -𝑛 -) -O(m×n), where -𝑚 -m is the number of rows and -𝑛 -n is the number of columns. Each cell is processed once, and boundary updates are linear. -Space Complexity: -𝑂 -( -𝑛 -) -O(n), for storing heights, leftBoundaries, and rightBoundaries. -Summary -This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. - - - - - - From 00bf7f74d765aae5431a2e42f3ce2499463ab19a Mon Sep 17 00:00:00 2001 From: Anany Dev Date: Mon, 4 Nov 2024 19:27:10 +0530 Subject: [PATCH 08/11] maximalRectangle --- maximal rectangle/README.md | 48 +++++++++++++++++++++++++ maximal rectangle/program.c | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 maximal rectangle/README.md create mode 100644 maximal rectangle/program.c diff --git a/maximal rectangle/README.md b/maximal rectangle/README.md new file mode 100644 index 00000000..a7e29f3e --- /dev/null +++ b/maximal rectangle/README.md @@ -0,0 +1,48 @@ +Problem Overview +The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. + +Approach +The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. + +Steps to Solve +Initialize Arrays: + +heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. +leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. +rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. +Row-by-Row Processing: + +For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. +Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. +Calculate Maximum Rectangle Area: + +For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). +Update maxRectangle with the maximum area encountered. +Helper Functions +updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. + +updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. + +calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. + +Complexity Analysis +Time Complexity: +𝑂 +( +𝑚 +× +𝑛 +) +O(m×n), where +𝑚 +m is the number of rows and +𝑛 +n is the number of columns. Each cell is processed once, and boundary updates are linear. +Space Complexity: +𝑂 +( +𝑛 +) +O(n), for storing heights, leftBoundaries, and rightBoundaries. +Summary +This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file diff --git a/maximal rectangle/program.c b/maximal rectangle/program.c new file mode 100644 index 00000000..a80dc100 --- /dev/null +++ b/maximal rectangle/program.c @@ -0,0 +1,71 @@ +#include +#include +#include + +int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize) { + if (matrix == NULL || matrixSize == 0 || matrixColSize[0] == 0) { + return 0; + } + + int m = matrixSize; + int n = matrixColSize[0]; + + int* heights = (int*)calloc(n, sizeof(int)); + int* leftBoundaries = (int*)calloc(n, sizeof(int)); + int* rightBoundaries = (int*)malloc(n * sizeof(int)); + for (int i = 0; i < n; i++) { + rightBoundaries[i] = n; + } + + int maxRectangle = 0; + + for (int i = 0; i < m; i++) { + int left = 0; + int right = n; + + updateHeightsAndLeftBoundaries(matrix[i], heights, leftBoundaries, n, &left); + + updateRightBoundaries(matrix[i], rightBoundaries, n, &right); + + maxRectangle = calculateMaxRectangle(heights, leftBoundaries, rightBoundaries, n, maxRectangle); + } + + free(heights); + free(leftBoundaries); + free(rightBoundaries); + + return maxRectangle; +} + +void updateHeightsAndLeftBoundaries(char* row, int* heights, int* leftBoundaries, int n, int* left) { + for (int j = 0; j < n; j++) { + if (row[j] == '1') { + heights[j]++; + leftBoundaries[j] = leftBoundaries[j] > *left ? leftBoundaries[j] : *left; + } else { + heights[j] = 0; + leftBoundaries[j] = 0; + *left = j + 1; + } + } +} + +void updateRightBoundaries(char* row, int* rightBoundaries, int n, int* right) { + for (int j = n - 1; j >= 0; j--) { + if (row[j] == '1') { + rightBoundaries[j] = rightBoundaries[j] < *right ? rightBoundaries[j] : *right; + } else { + rightBoundaries[j] = n; + *right = j; + } + } +} + +int calculateMaxRectangle(int* heights, int* leftBoundaries, int* rightBoundaries, int n, int maxRectangle) { + for (int j = 0; j < n; j++) { + int width = rightBoundaries[j] - leftBoundaries[j]; + int area = heights[j] * width; + maxRectangle = maxRectangle > area ? maxRectangle : area; + } + return maxRectangle; +} \ No newline at end of file From 6be28d89d2b7a90d1798d342bb6e56ca20b446f5 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind <73558583+pankaj-bind@users.noreply.github.com> Date: Tue, 5 Nov 2024 04:08:29 +0530 Subject: [PATCH 09/11] Delete Graph Algorithms/Swim in Rising Water/program.c --- .../Swim in Rising Water/program.c | 100 ------------------ 1 file changed, 100 deletions(-) delete mode 100644 Graph Algorithms/Swim in Rising Water/program.c diff --git a/Graph Algorithms/Swim in Rising Water/program.c b/Graph Algorithms/Swim in Rising Water/program.c deleted file mode 100644 index ed7c86c7..00000000 --- a/Graph Algorithms/Swim in Rising Water/program.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include -#include - -int **arr; -int n; -int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; - -// Function to perform BFS traversal -bool helper(int x) { - if (arr[0][0] > x) return false; - - int **visited = (int **)malloc(n * sizeof(int *)); - for (int i = 0; i < n; i++) { - visited[i] = (int *)calloc(n, sizeof(int)); - } - - int queue[n * n][2]; - int front = 0, rear = 0; - queue[rear][0] = 0; - queue[rear++][1] = 0; - visited[0][0] = 1; - - while (front < rear) { - int r = queue[front][0]; - int c = queue[front++][1]; - - if (r == n - 1 && c == n - 1) - { - for (int i = 0; i < n; i++) { - free(visited[i]); - } - free(visited); - return true; - } - - for (int i = 0; i < 4; i++) { - int nr = r + dir[i][0]; - int nc = c + dir[i][1]; - if (nr >= 0 && nc >= 0 && nr < n && nc < n && visited[nr][nc] == 0 && arr[nr][nc] <= x) { - queue[rear][0] = nr; - queue[rear++][1] = nc; - visited[nr][nc] = 1; - } - } - } - - for (int i = 0; i < n; i++) { - free(visited[i]); - } - free(visited); - - return false; -} - -// Main function to solve the swim in water problem -int swimInWater(int **grid, int gridSize) { - arr = grid; - n = gridSize; - int st = 0, end = 2505, ans = -1; - - while (st <= end) { - int mid = st + (end - st) / 2; - if (helper(mid)) { - ans = mid; - end = mid - 1; - } else { - st = mid + 1; - } - } - return ans; -} - -// Main function -int main() { - printf("Enter the grid size (n x n): "); - scanf("%d", &n); - - arr = (int **)malloc(n * sizeof(int *)); - for (int i = 0; i < n; i++) { - arr[i] = (int *)malloc(n * sizeof(int)); - } - - printf("Enter the grid elements:\n"); - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - scanf("%d", &arr[i][j]); - } - } - - int result = swimInWater(arr, n); - printf("Minimum time to swim to the bottom-right corner: %d\n", result); - - for (int i = 0; i < n; i++) { - free(arr[i]); - } - free(arr); - - return 0; -} From 9b1d80ea7f33e8641e785377b7e46752403012ba Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind <73558583+pankaj-bind@users.noreply.github.com> Date: Tue, 5 Nov 2024 04:08:41 +0530 Subject: [PATCH 10/11] Delete maximal rectangle/README.md --- maximal rectangle/README.md | 48 ------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 maximal rectangle/README.md diff --git a/maximal rectangle/README.md b/maximal rectangle/README.md deleted file mode 100644 index a7e29f3e..00000000 --- a/maximal rectangle/README.md +++ /dev/null @@ -1,48 +0,0 @@ -Problem Overview -The task is to find the largest rectangle containing only 1's in a given binary matrix. Each element in the matrix is either '0' or '1', and we want to calculate the maximum area of contiguous '1's that can form a rectangle. - -Approach -The problem can be approached using the concept of dynamic histogram heights, where each row of the matrix is treated as a base and we calculate the histogram height of each column based on the presence of '1's in the rows above it. This allows us to use techniques similar to finding the largest rectangle in a histogram for each row, which can be efficiently solved with the following steps. - -Steps to Solve -Initialize Arrays: - -heights[]: Stores the height of '1's for each column. At any row i, if matrix[i][j] is '1', the height at heights[j] is incremented; otherwise, it's reset to 0. -leftBoundaries[]: Tracks the left boundary (leftmost column where '1's can extend) for each column in the current row. -rightBoundaries[]: Tracks the right boundary (rightmost column where '1's can extend) for each column in the current row. -Row-by-Row Processing: - -For each row, update the heights[], leftBoundaries[], and rightBoundaries[] arrays. -Calculate the maximum area for each rectangle ending at each column by multiplying the height by the width defined by the difference between the right and left boundaries. -Calculate Maximum Rectangle Area: - -For each column, calculate the rectangle area using the formula area = height * (rightBoundary - leftBoundary). -Update maxRectangle with the maximum area encountered. -Helper Functions -updateHeightsAndLeftBoundaries: Updates the heights[] array and leftBoundaries[] for the current row. If matrix[i][j] is '1', heights[j] is incremented, and leftBoundaries[j] is updated based on the current left boundary. Otherwise, heights[j] is reset to 0, and the left boundary is updated. - -updateRightBoundaries: Updates the rightBoundaries[] array for the current row. If matrix[i][j] is '1', rightBoundaries[j] is updated based on the current right boundary. If not, rightBoundaries[j] is reset, and right is adjusted. - -calculateMaxRectangle: Iterates over each column and calculates the area for each possible rectangle using heights, leftBoundaries, and rightBoundaries. It then updates maxRectangle if a larger area is found. - -Complexity Analysis -Time Complexity: -𝑂 -( -𝑚 -× -𝑛 -) -O(m×n), where -𝑚 -m is the number of rows and -𝑛 -n is the number of columns. Each cell is processed once, and boundary updates are linear. -Space Complexity: -𝑂 -( -𝑛 -) -O(n), for storing heights, leftBoundaries, and rightBoundaries. -Summary -This approach leverages a dynamic programming technique to keep track of heights and boundaries, transforming the problem into a maximal rectangle calculation on each row as if it were a histogram. By iteratively updating the boundaries and heights, we efficiently compute the maximal rectangle area in a binary matrix. This method ensures that the solution is optimal for large matrices while maintaining manageable space complexity. \ No newline at end of file From 864b361bfb32fbaf1d786cd36a41678160507344 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind <73558583+pankaj-bind@users.noreply.github.com> Date: Tue, 5 Nov 2024 04:08:54 +0530 Subject: [PATCH 11/11] Delete maximal rectangle/program.c --- maximal rectangle/program.c | 71 ------------------------------------- 1 file changed, 71 deletions(-) delete mode 100644 maximal rectangle/program.c diff --git a/maximal rectangle/program.c b/maximal rectangle/program.c deleted file mode 100644 index a80dc100..00000000 --- a/maximal rectangle/program.c +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include - -int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize) { - if (matrix == NULL || matrixSize == 0 || matrixColSize[0] == 0) { - return 0; - } - - int m = matrixSize; - int n = matrixColSize[0]; - - int* heights = (int*)calloc(n, sizeof(int)); - int* leftBoundaries = (int*)calloc(n, sizeof(int)); - int* rightBoundaries = (int*)malloc(n * sizeof(int)); - for (int i = 0; i < n; i++) { - rightBoundaries[i] = n; - } - - int maxRectangle = 0; - - for (int i = 0; i < m; i++) { - int left = 0; - int right = n; - - updateHeightsAndLeftBoundaries(matrix[i], heights, leftBoundaries, n, &left); - - updateRightBoundaries(matrix[i], rightBoundaries, n, &right); - - maxRectangle = calculateMaxRectangle(heights, leftBoundaries, rightBoundaries, n, maxRectangle); - } - - free(heights); - free(leftBoundaries); - free(rightBoundaries); - - return maxRectangle; -} - -void updateHeightsAndLeftBoundaries(char* row, int* heights, int* leftBoundaries, int n, int* left) { - for (int j = 0; j < n; j++) { - if (row[j] == '1') { - heights[j]++; - leftBoundaries[j] = leftBoundaries[j] > *left ? leftBoundaries[j] : *left; - } else { - heights[j] = 0; - leftBoundaries[j] = 0; - *left = j + 1; - } - } -} - -void updateRightBoundaries(char* row, int* rightBoundaries, int n, int* right) { - for (int j = n - 1; j >= 0; j--) { - if (row[j] == '1') { - rightBoundaries[j] = rightBoundaries[j] < *right ? rightBoundaries[j] : *right; - } else { - rightBoundaries[j] = n; - *right = j; - } - } -} - -int calculateMaxRectangle(int* heights, int* leftBoundaries, int* rightBoundaries, int n, int maxRectangle) { - for (int j = 0; j < n; j++) { - int width = rightBoundaries[j] - leftBoundaries[j]; - int area = heights[j] * width; - maxRectangle = maxRectangle > area ? maxRectangle : area; - } - return maxRectangle; -} \ No newline at end of file