I'm sure most of you have heard of the biggest rectangle in the histogram problem. - Link -
In my current project, I need to modify this algorithm to detect all rectangles that are not a smaller subset of the other rectangle in this histogram.
That's how far I am now. But I can’t figure out how to disregard the subsets here.
//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, #.
, (, java) Rect.Add(new [] {p, h, w}.).
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} .. ; .. .
Source: https://habr.com/ru/post/1692471/More articles:ES6 - ES5: implementation of the Babel class extension - javascriptВложенные генераторы и выход из? - pythonHow to work with scipy and extremely large numbers - pythonVue: How do you add E2E tests without including them in the original web package template? - webpackHow to remove React Native StackNavigator inner shadow when using safeAreaView together - react-nativeName or service unknown [tcp: // redis: 6379] - phpHow to transfer the list by reference? - pythonhow to say C ++ to discard a specific element in a vector according to the index pointer - c ++implementation of the apply function in Rcpp - c ++Get the body of an HTML table in Python using Selenium - pythonAll Articles