Trapping Rainwater in c++

 

Trapping Rainwater  in c++

The Largest Rectangle in a Histogram (LRH) problem is a classic problem in computer science and is often used as an example of how to use stack data structures. The problem consists of finding the largest rectangle that can be formed using the bars of a histogram. The histogram is represented by an array of integers, where each element represents the height of a bar. The goal is to find the largest rectangle that can be formed using the bars of the histogram, where the rectangle can be formed by selecting one or more bars and forming a rectangle with the height of the tallest bar and the width of the number of bars selected.

The LRH problem can be solved using a stack data structure, which is a last-in, first-out (LIFO) data structure. The stack is used to store the indices of the bars in the histogram. The algorithm starts by pushing the first index (0) onto the stack. Then, it iterates through the array of histogram heights, comparing each element to the element on the top of the stack. If the current element is greater than or equal to the element on the top of the stack, the current index is pushed onto the stack. If the current element is less than the element on the top of the stack, the algorithm pops elements off the stack until the element on the top of the stack is less than or equal to the current element. The algorithm then calculates the area of the rectangle formed by the popped elements, and compares it to the current maximum area. This process is repeated until the end of the array is reached.

One of the key elements of this algorithm is the use of the stack data structure. The stack allows for efficient calculation of the area of rectangles formed by popped elements, as well as efficient tracking of the current maximum area. Additionally, the use of the stack allows for the algorithm to handle the case where the histogram contains multiple bars of the same height, as the algorithm will continue to pop elements off the stack until the element on top is less than or equal to the current element.

The LRH problem can also be solved using dynamic programming, where the algorithm builds a solution by solving subproblems and storing the results in a table. The dynamic programming algorithm starts by creating a table with the same number of rows and columns as the histogram. The algorithm then iterates through the histogram, calculating the maximum area for each element and storing the result in the corresponding cell of the table. The algorithm then iterates through the table, comparing the maximum area for each element to the current maximum area, and updating the current maximum area if necessary.

The dynamic programming algorithm has a time complexity of O(n^2), where n is the number of elements in the histogram. This is because the algorithm must iterate through the histogram twice, once to calculate the maximum area for each element and once to find the current maximum area. Additionally, the algorithm requires additional space to store the results in the table, which increases the space complexity of the algorithm.

In C++, the Largest Rectangle in a Histogram problem can be implemented using both stack and dynamic programming algorithms. The following code demonstrates how the stack algorithm can be implemented in C++:

#include <iostream> 
 #include <stack> 
 using namespace std; 
 int largestRectangleArea(int* heights, int n) {
 stack<int> s;
 int maxArea = 0
 for (int i = 0; i <= n; i++) {  
while (!s.empty() && (i == n || heights[s.top()] > heights[i])) {
 int h = heights[s.top()];
 s.pop(); 
 int w = s.empty() ? i : i - s.top() - 1;
 maxArea = max(maxArea, h * w);
 }
 s.push(i); } return maxArea;
 }
 int main()
 int heights[] = {2, 1, 5, 6, 2, 3}; int n = sizeof(heights) / sizeof(heights[0]);
 cout << "Largest rectangle area in the histogram is: " << largestRectangleArea(heights, n) << endl; return 0
}

This code defines the largestRectangleArea() function, which takes an array of integers representing the heights of the histogram bars and the number of elements in the array. The function uses a stack to store the indices of the bars in the histogram. The algorithm starts by pushing the first index (0) onto the stack. Then, it iterates through the array of histogram heights, comparing each element to the element on the top of the stack. If the current element is greater than or equal to the element on the top of the stack, the current index is pushed onto the stack. If the current element is less than the element on the top of the stack, the algorithm pops elements off the stack until the element on the top of the stack is less than or equal to the current element. The algorithm then calculates the area of the rectangle formed by the popped elements, and compares it to the current maximum area. This process is repeated until the end of the array is reached. In the main function, the code creates an example histogram array and calls the largestRectangleArea() function, printing the result.

On the other hand, the following code demonstrates how the dynamic programming algorithm can be implemented in C++:

#include <iostream>
  using namespace std;
 int largestRectangleArea(int* heights, int n) {
 int maxArea = 0; int dp[n];
 for (int i = 0; i < n; i++) {
 dp[i] = heights[i];
 } 
 for (int i = 0; i < n; i++) {
 for (int j = i; j >= 0; j--) {
if (heights[j] > dp[i]) {
 dp[i] = heights[j];
 }
 maxArea = max(maxArea, (i - j + 1) * dp[i]);
 } 
 }
 return maxArea; 
 int main() {
 int heights[] = {2, 1, 5, 6, 2, 3};
 int n = sizeof(heights) / sizeof(heights[0]);
 cout << "Largest rectangle area in the histogram is: " << largestRectangleArea(heights, n) << endl;