java – 查找直方图中的所有矩形

我相信大多数人都听说过直方图问题中最大的矩形. –Link

在我目前的项目中,我需要更改此算法,以便找到所有矩形,这些矩形不是该直方图中另一个矩形的较小子集.

这是我目前的程度.但我无法弄清楚如何不计算这里的子集.

   //time: O(n), space:O(n)
     public ArrayList<int[]> largestRectangles(int[] height) {
            ArrayList<int[]> listRect = new ArrayList<int[]>();

            if (height == null || height.length == 0) {
                return null;
            }

            Stack<Integer> stack = new Stack<Integer>();

            int max = 0;
            int i = 0;

            while (i < height.length) {
                //push index to stack when the current height is larger than the previous one
                if (stack.isEmpty() || height[i] >= height[stack.peek()]) {
                    stack.push(i);
                    i++;
                } else {
                //add rectangle when the current height is less than the previous one
                    int p = stack.pop();
                    int h = height[p];
                    int w = stack.isEmpty() ? i : i - stack.peek() - 1;
                    listRect.add(new int[]{p,h,w});
                }

            }

            while (!stack.isEmpty()) {
                int p = stack.pop();
                int h = height[p];
                int w = stack.isEmpty() ? i : i - stack.peek() - 1;
                listRect.add(new int[]{p,h,w});
            }

            return listRect;
        }
public static void main(String[] args) {

         for(int[] rect : largestRectangles(new int[]{1,2,2,3,3,2})) {
             System.out.print("pos:"+rect[0]+" height"+rect[1]+" 
         width"+rect[2]);
             System.out.println();
         }
     }

解决方法:

我们的想法是检查添加的新矩形是否包含最后添加的矩形;如果是这样,那么只需在添加新的矩形信息之前从结果列表中删除最后添加的矩形信息(因此按高度确认).我没有Java IDE,所以在C#中尝试过.

以下是您需要在listRect.Add(new [] {p,h,w}之前)添加到两个地方(请转换为java)的部分.).

if (listRect.Count > 0)
{

    if (listRect[listRect.Count - 1][1] <= h)
    {
        listRect.RemoveAt(listRect.Count - 1);
    }
}

这只是一个想法.您必须编写逻辑以省略上面删除逻辑的直方图,其中包含0,即new int [] {1,2,2,3,3,2,0,1}等.但逻辑类似;你必须存储一个标志等,并根据它的位置绕过去除最后一个矩形.

上一篇:在Android中从RectF转换为Rect的最佳方法是什么?


下一篇:spring+mybatis项目启动报错Initializing Spring root WebApplicationContext