http://www.cnblogs.com/Gryffin/p/7468676.html
Given an array of rectangles consisting of left top and right down points [[x1,y1, x2, y2],[x3,y3,x4,y4],...] , find any point that is covered by most rectangles.
Given an array of rectangles consisting of left top and right down points [[x1,y1, x2, y2],[x3,y3,x4,y4],...] , find any point that is covered by most rectangles.
public static class Rectangle { int x1; int y1; int x2; int y2; Rectangle(int x1, int y1, int x2, int y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } @Override public String toString() { return x1 + "|" + y1 + "|" + x2 + "|" + y2; } } public int[] getArea(List<Rectangle> rects) { Set<Integer> ys = new HashSet<Integer>(); Map<Integer, List<Rectangle>> map = new HashMap<Integer, List<Rectangle>>(); for (Rectangle rect : rects) { ys.add(rect.y1); ys.add(rect.y2); List<Rectangle> list = map.getOrDefault(rect.y1, new ArrayList<>()); list.add(rect); map.put(rect.y1, list); list = map.getOrDefault(rect.y2, new ArrayList<>()); list.add(rect); map.put(rect.y2, list); } List<Integer> yList = new ArrayList<Integer>(ys); Collections.sort(yList); PriorityQueue<Rectangle> pq = new PriorityQueue<Rectangle>((o1, o2) -> Integer.compare(o1.y2, o2.y2)); int ans = 0; int ans_x = 0; int ans_y = 0; for (int y : yList) { List<Rectangle> list = map.get(y); if (list == null) { continue; } for (Rectangle rect : list) { if (rect.y1 == y) { pq.add(rect); } } int[] starts = new int[pq.size()]; int[] ends = new int[pq.size()]; int counter = 0; System.out.println(pq.toString()); for (Rectangle rect : pq) { starts[counter] = rect.x1; ends[counter++] = rect.x2; } Arrays.sort(starts); Arrays.sort(ends); int r = 0; int index = 0; int pos = 0; for (int i = 0; i < starts.length; i++) { if (starts[i] < ends[index]) { r++; pos = ends[index]; } else { index++; } } if (r > ans) { ans = r; ans_x = pos; ans_y = y; } while (!pq.isEmpty() && pq.peek().y2 <= y) { pq.poll(); } } return new int[] {ans_x, ans_y, ans}; }
Given an array of rectangles consisting of left top and right down points [[x1,y1, x2, y2],[x3,y3,x4,y4],...] , find the area covered by all these rectangles.
public static class Rectangle { int x1; int y1; int x2; int y2; Rectangle(int x1, int y1, int x2, int y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } @Override public String toString() { return x1 + "|" + y1 + "|" + x2 + "|" + y2; } } public int getArea2(List<Rectangle> rects) { int ans = 0; Set<Integer> ys = new HashSet<Integer>(); Map<Integer, List<Rectangle>> map = new HashMap<Integer, List<Rectangle>>(); for (Rectangle rect : rects) { ys.add(rect.y1); ys.add(rect.y2); List<Rectangle> list = map.getOrDefault(rect.y1, new ArrayList<>()); list.add(rect); map.put(rect.y1, list); list = map.getOrDefault(rect.y2, new ArrayList<>()); list.add(rect); map.put(rect.y2, list); } System.out.println(map.toString()); List<Integer> yList = new ArrayList<Integer>(ys); Collections.sort(yList); System.out.println(yList.toString()); PriorityQueue<Rectangle> pq = new PriorityQueue<Rectangle>((o1, o2) -> Integer.compare(o1.y2, o2.y2)); for (int i = 0; i < yList.size(); i++) { int y = yList.get(i); List<Rectangle> list = map.get(y); if (list == null) { continue; } System.out.println(pq.toString()); if (!pq.isEmpty()) { int[] starts = new int[pq.size()]; int[] ends = new int[pq.size()]; int counter = 0; int deltaY = yList.get(i) - yList.get(i - 1); for (Rectangle rect : pq) { starts[counter] = rect.x1; ends[counter++] = rect.x2; } Arrays.sort(starts); Arrays.sort(ends); int a = 0; int b = 0; int r = 0; while (a < starts.length) { if (starts[a] < ends[b]) { if (r == 0) { ans += (ends[b] - starts[a]) * deltaY; System.out.println("ans: " + ans); } a++; r++; } else { r--; b++; } } while (!pq.isEmpty() && pq.peek().y2 == y) { pq.poll(); } } for (Rectangle rect : list) { if (rect.y1 == y) { pq.add(rect); } } } return ans; }