Maximum Perimeter of Histogram: Efficient Algorithms

Maximum Perimeter of Histogram: Efficient Algorithms
Slide Note
Embed
Share

Efficient algorithms for finding the maximum perimeter of a histogram formed by a series of rectangles with different heights. Learn about backtracking and bitmasking approaches to solve this problem effectively.

  • Algorithms
  • Histogram
  • Perimeter
  • Backtracking
  • Bitmasking

Uploaded on Feb 19, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Question: Given n rectangles each having different height and are 1 unit wide. Find the maximum perimeter of the histogram formed by using these n rectangles. If heights are 1,2,3,4. Two possible histograms are shown in right.The maximum possible perimeter for heights 1,2,3,4 is 20.

  2. Approach for the question For every rectangle we calculate the contribution on the left side which is equal to absolute difference of the current rectangle and previous rectangle curr prev curr prev (a) (b)

  3. The width of the histogram will remain same that is equal to number of rectangles so we can just add it in the end. For starting rectangle we can consider the height of previous rectangle equal to 0 unit. For last rectangle we will just add the height of the rectangle to answer.

  4. Code using backtracking int arr[20]; int maxPerimeter(int pre,int rectanglesCovered,int visited[],int n){ if(rectanglesCovered==n){ return arr[pre]; } int ans=0; for(int j=1; j<=n; j++){ if(visited[j]==-1){ visited[j]=1; ans=max(ans,abs(arr[pre]- arr[j])+maxPerimeter(j,rectanglesCovered+1,visited,n)); visited[j]=-1; } } return ans; }

  5. Code using bitmasking int arr[20]; int dp[100005][20]; int maxPerimeter(int mask,int pre,int n){ //number of rectangles covered int rectanglesCovered=__builtin_popcount(mask); if(rectanglesCovered==n){ return arr[pre]; } if(dp[mask][pre]!=-1) return dp[mask][pre]; int ans=0; // taking the rectngles which have not been considered till now for(int j=1; j<=n; j++){ if((mask&(1<<j))==0){ ans=max(ans,abs(arr[pre]-arr[j])+maxPerimeter((mask|(1<<j)),j,n)); } } return dp[mask][pre]=ans; }

  6. Basic Observations Bit masking takes exponential complexity, so generally if the constraints are around 15-20 we can think of bit masking. While using left shift operator i.e. 1<<j prefer using 1LL<<j since it has a range equal to long long int.

  7. HomeWork There is an extension to this question, you have to find the maximum possible perimeter and also count the number of permutations having maximum perimeter. https://www.spoj.com/problems/HIST2/

More Related Content