|
| 1 | +/** |
| 2 | + * 3047. Find the Largest Area of Square Inside Two Rectangles |
| 3 | + * https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You |
| 7 | + * are given two 2D integer arrays bottomLeft and topRight where bottomLeft[i] = [a_i, b_i] |
| 8 | + * and topRight[i] = [c_i, d_i] represent the bottom-left and top-right coordinates of the |
| 9 | + * ith rectangle, respectively. |
| 10 | + * |
| 11 | + * You need to find the maximum area of a square that can fit inside the intersecting |
| 12 | + * region of at least two rectangles. Return 0 if such a square does not exist. |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {number[][]} bottomLeft |
| 17 | + * @param {number[][]} topRight |
| 18 | + * @return {number} |
| 19 | + */ |
| 20 | +var largestSquareArea = function(bottomLeft, topRight) { |
| 21 | + const n = bottomLeft.length; |
| 22 | + let maxSide = 0; |
| 23 | + |
| 24 | + for (let i = 0; i < n; i++) { |
| 25 | + for (let j = i + 1; j < n; j++) { |
| 26 | + const xLeft = Math.max(bottomLeft[i][0], bottomLeft[j][0]); |
| 27 | + const yBottom = Math.max(bottomLeft[i][1], bottomLeft[j][1]); |
| 28 | + const xRight = Math.min(topRight[i][0], topRight[j][0]); |
| 29 | + const yTop = Math.min(topRight[i][1], topRight[j][1]); |
| 30 | + |
| 31 | + if (xLeft < xRight && yBottom < yTop) { |
| 32 | + const side = Math.min(xRight - xLeft, yTop - yBottom); |
| 33 | + maxSide = Math.max(maxSide, side); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return maxSide * maxSide; |
| 39 | +}; |
0 commit comments