Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6c1c55a

Browse files
committed
Add solution #3047
1 parent 8a0613b commit 6c1c55a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,7 @@
21492149
3043|[Find the Length of the Longest Common Prefix](./solutions/3043-find-the-length-of-the-longest-common-prefix.js)|Medium|
21502150
3044|[Most Frequent Prime](./solutions/3044-most-frequent-prime.js)|Medium|
21512151
3046|[Split the Array](./solutions/3046-split-the-array.js)|Easy|
2152+
3047|[Find the Largest Area of Square Inside Two Rectangles](./solutions/3047-find-the-largest-area-of-square-inside-two-rectangles.js)|Medium|
21522153
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21532154
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
21542155
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)