Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 给定N个建筑,找出连续建筑形成的最大固体面积_Algorithm_Data Structures_Stack_Dynamic Programming - Fatal编程技术网

Algorithm 给定N个建筑,找出连续建筑形成的最大固体面积

Algorithm 给定N个建筑,找出连续建筑形成的最大固体面积,algorithm,data-structures,stack,dynamic-programming,Algorithm,Data Structures,Stack,Dynamic Programming,问题陈述:在某个一维景观中有N座建筑。每栋建筑都有一个高度,由hi,i给出∈[1,N]。如果连接K个相邻的建筑,它们将形成一个面积为K×min(hi,hi+1,…,hi+K)的实心矩形−1) 给定N个建筑,找出连续建筑形成的最大固体面积 下面是我的解决方案。在我看来,它很好,但在测试用例中失败了。 谁能告诉我下面的解决方案有什么问题。 public class Solution { public static void main(String[] args) { In

问题陈述:在某个一维景观中有N座建筑。每栋建筑都有一个高度,由hi,i给出∈[1,N]。如果连接K个相邻的建筑,它们将形成一个面积为K×min(hi,hi+1,…,hi+K)的实心矩形−1)

给定N个建筑,找出连续建筑形成的最大固体面积

下面是我的解决方案。在我看来,它很好,但在测试用例中失败了。 谁能告诉我下面的解决方案有什么问题。

public class Solution {

    public static void main(String[] args) {
        InputReader ir = new InputReader(System.in);
        int N = ir.nextInt();
        long [] hts = new long[N];
        long [] res = new long[N+1];
        for(int i=0; i<N; i++){
            hts[i] = ir.nextLong();            
        }

        //Computes max area for subset of length 1
        long max = Integer.MIN_VALUE;
        for(int i=1; i<=N; i++){
            if(max<hts[i-1]){
                 max = hts[i-1];
            }
        }

        res[1] = max;
        /* Computes max of all the minimum heights 
         * of subsets of length i and 
         * store area at res[i] position
         */
        for(int i=2; i<=N; i++){
            max = Integer.MIN_VALUE;
            for(int j=0; j<N-i+1; j++){
                long min = hts[j];
                for(int k=j+1;k<i-j;k++){
                    if(hts[k]<min){
                        min = hts[k];
                    }
                }
                if(min>max){
                    max = min;
                }                
            }            
            res[i] = i*max;
        }

       // Get maximum area
        long result = res[1];
        for(int i=2; i<N+1;i++){
            if((res[i])>result){
                result = res[i];
            }
        }

        System.out.println(result);
    }
}
公共类解决方案{
公共静态void main(字符串[]args){
InputReader ir=新的InputReader(System.in);
int N=ir.nextInt();
long[]hts=新长[N];
long[]res=新长[N+1];

对于(int i=0;i有比暴力更快的解决方案,只需将每个建筑映射到它的对应物,如下所示:

           _____
      _____|   |
______|<--||-->|_____
|<---||---||---||-->|
|    ||   ||   ||   |
int getMaxArea(int[] in)
    map height_to_maxw

    stack last_height
    stack at_pos

    int max_area = 0

    for int i in [0 , length(in))
        if !last_heigth.isEmpty()
            while last_height.peek() > in[i]
                int l_h = last_height.pop()
                int l_w = i - at_pos.pop()

                if l_h * l_w > max_area
                    max_area = l_h * l_w

    //check if the widest area is the greatest
    int h = min(in[0] , in[length(in) - 1])
    if h * length(in) > max_area
        max_area = h * length(in)

    return max_area