3047. Find the Largest Area of Square Inside Two Rectangles
</> Solution
/**
* Problem: LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
long maxSide = 0;
int n = bottomLeft.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int xMin = Math.max(bottomLeft[i][0], bottomLeft[j][0]);
int xMax = Math.min(topRight[i][0], topRight[j][0]);
int yMin = Math.max(bottomLeft[i][1], bottomLeft[j][1]);
int yMax = Math.min(topRight[i][1], topRight[j][1]);
int width = xMax - xMin;
int height = yMax - yMin;
if (width > 0 && height > 0) {
long side = Math.min(width, height);
if (side > maxSide) {
maxSide = side;
}
}
}
}
return maxSide * maxSide;
}
}
"""
Problem: LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def largestSquareArea(
self, bottomLeft: List[List[int]], topRight: List[List[int]]
) -> int:
max_side = 0
n = len(bottomLeft)
for i in range(n):
for j in range(i + 1, n):
x_min = max(bottomLeft[i][0], bottomLeft[j][0])
x_max = min(topRight[i][0], topRight[j][0])
y_min = max(bottomLeft[i][1], bottomLeft[j][1])
y_max = min(topRight[i][1], topRight[j][1])
width = x_max - x_min
height = y_max - y_min
if width > 0 and height > 0:
side = min(width, height)
if side > max_side:
max_side = side
return max_side * max_side
/**
* Problem: LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) {
long long maxSide = 0;
int n = static_cast<int>(bottomLeft.size());
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int xMin = max(bottomLeft[i][0], bottomLeft[j][0]);
int xMax = min(topRight[i][0], topRight[j][0]);
int yMin = max(bottomLeft[i][1], bottomLeft[j][1]);
int yMax = min(topRight[i][1], topRight[j][1]);
int width = xMax - xMin;
int height = yMax - yMin;
if (width > 0 && height > 0) {
long long side = min(width, height);
if (side > maxSide) {
maxSide = side;
}
}
}
}
return maxSide * maxSide;
}
};
/**
* Problem: LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} bottomLeft
* @param {number[][]} topRight
* @return {number}
*/
var largestSquareArea = function(bottomLeft, topRight) {
let maxSide = 0n;
const n = bottomLeft.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const xMin = Math.max(bottomLeft[i][0], bottomLeft[j][0]);
const xMax = Math.min(topRight[i][0], topRight[j][0]);
const yMin = Math.max(bottomLeft[i][1], bottomLeft[j][1]);
const yMax = Math.min(topRight[i][1], topRight[j][1]);
const width = xMax - xMin;
const height = yMax - yMin;
if (width > 0 && height > 0) {
const side = BigInt(Math.min(width, height));
if (side > maxSide) {
maxSide = side;
}
}
}
}
return Number(maxSide * maxSide);
};