多边形由不同宽度和高度的垂直相邻塔组成。找到适合此多边形的矩形的最大面积

时间:2018-10-09 03:51:16

标签: java

我正在尝试找出可以容纳在多边形内部的矩形的最大可能区域。该多边形由相邻的垂直塔组成,每个塔都有自己的宽度和高度。

基本上,我会得到塔的宽度高度对的数量,然后是实际的宽度和高度值,并且需要使用此数据来找到enter code here矩形的最大可能面积以适合内部多边形。

import java.util.*;
public class LargestRectInHist {
	
	
	// The main function to find the maximum rectangular area under histogram
    
    static int getMaxArea(int heights[], int size)  
    { 
        // Create an empty stack. The stack holds indexes of heights[] array 
        
        Stack<Integer> stack = new Stack<>(); 
          
        int max_area = 0; // Stores the current highest area found in heights[]
        int top;  // Stores the current height at the top of the stack 
        int area=0; // Stores current area
       
        // Run through all bars of given histogram 
        int i = 0; 
        while (i < size) 
        { 
            // If this bar is higher than the bar on top of the stack, push it on the stack 
            if (stack.empty() || heights[stack.peek()] <= heights[i]) 
                stack.push(i++); 
            
       
            // If this bar is lower than the top of the stack, then calculate the area of the rectangle  
            // with the top of the stack as the smallest (or minimum height) bar. 'i' is  
            // 'right index' for the top and element before the top of the stack is 'left index' (or stack.peek())
            else
            { 
                top = stack.peek();  // store the top index 
                stack.pop();  // pop the top 
       
                // Calculate the area with heights[top] stack as smallest bar 
                
                
                
                if(stack.empty()) {
                	area = heights[top] * i;
                }else {
                	area = heights[top] * (i-stack.peek()-1);
                }
               
                
      // update max area, if needed 
                if (max_area < area) 
                    max_area = area; 
            } 
        } 
       
        // Now pop the remaining bars from stack and calculate area with every 
        // popped bar as the smallest bar 
        while (stack.empty() == false) 
        { 
            top = stack.peek(); 
            stack.pop(); 
            
            
            if(stack.empty()) {
            	area = heights[top] * i;
            }else {
            	area = heights[top] * (i-stack.peek()-1);
            }
           
           
            if (max_area < area) 
                max_area = area; 
        } 
       
        return max_area; 
  
    } 
    
   
    public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter the size of your sequence:");
		int size = scan.nextInt();
		
		int heights[] = new int[size]; 
		System.out.println("Please enter the list of integers separated by a space");
		
		for(int i = 0; i < size; i++){
	        heights[i] = scan.nextInt();
	      }
		
        System.out.println("Maximum area is " + getMaxArea(heights, size)); 

	}

}

我知道如何解决这个问题,只要给我们塔的高度,并假设它们的所有宽度都是一个(如上面的代码所示)。我遇到的问题是将宽度合并到算法中以找到最大面积。关于如何调整我的算法有什么想法吗?

0 个答案:

没有答案