Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created January 17, 2026 17:24
Show Gist options
  • Select an option

  • Save tatsuyax25/1ce176ecf860f309a6c8b6e3ff85fdf9 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/1ce176ecf860f309a6c8b6e3ff85fdf9 to your computer and use it in GitHub Desktop.
There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays bottomLeft and topRight where bottomLeft[i] = [a_i, b_i] and topRight[i] = [c_i, d_i] represent the bottom-left and top-right coordina
/**
* @param {number[][]} bottomLeft
* @param {number[][]} topRight
* @return {number}
*/
var largestSquareArea = function(bottomLeft, topRight) {
let n = bottomLeft.length;
let maxArea = 0;
// Compare every pair of rectangles (i, j)
for (let i = 0; i < n; i++) {
const [a1, b1] = bottomLeft[i];
const [c1, d1] = topRight[i];
for (let j = i + 1; j < n; j++) {
const [a2, b2] = bottomLeft[j];
const [c2, d2] = topRight[j];
// Compute intersection boundaries
const left = Math.max(a1, a2);
const right = Math.min(c1, c2);
const bottom = Math.max(b1, b2);
const top = Math.min(d1, d2);
// Compute intersection width/height
const width = right - left;
const height = top - bottom;
// If no valid intersection, skip
if (width <= 0 || height <= 0) continue;
// Largest square that fits inside this intersection
const side = Math.min(width, height);
const area = side * side;
// Update global maximum
maxArea = Math.max(maxArea, area);
}
}
return maxArea;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment