diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README.md b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README.md index f06e9da6e282b..e9bea7d126e49 100644 --- a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README.md +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README.md @@ -95,32 +95,189 @@ tags: -### 方法一 +### 方法一:枚举 + +我们可以枚举矩形的左下角下标 $(x_3, y_3)$ 和右上角下标 $(x_4, y_4)$,然后枚举所有的点 $(x, y)$,判断点是否在矩形的内部或边界上,如果是,说明不满足条件,否则,我们排除掉在矩形外部的点,然后判断剩下的点是否有 4 个,如果有,说明这 4 个点可以构成一个矩形,计算矩形的面积,取最大值即可。 + +时间复杂度 $O(n^3)$,其中 $n$ 是数组 $\textit{points}$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def maxRectangleArea(self, points: List[List[int]]) -> int: + def check(x1: int, y1: int, x2: int, y2: int) -> bool: + cnt = 0 + for x, y in points: + if x < x1 or x > x2 or y < y1 or y > y2: + continue + if (x == x1 or x == x2) and (y == y1 or y == y2): + cnt += 1 + continue + return False + return cnt == 4 + + ans = -1 + for i, (x1, y1) in enumerate(points): + for x2, y2 in points[:i]: + x3, y3 = min(x1, x2), min(y1, y2) + x4, y4 = max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4): + ans = max(ans, (x4 - x3) * (y4 - y3)) + return ans ``` #### Java ```java - +class Solution { + public int maxRectangleArea(int[][] points) { + int ans = -1; + for (int i = 0; i < points.length; ++i) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; ++j) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = Math.min(x1, x2), y3 = Math.min(y1, y2); + int x4 = Math.max(x1, x2), y4 = Math.max(y1, y2); + if (check(points, x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } + + private boolean check(int[][] points, int x1, int y1, int x2, int y2) { + int cnt = 0; + for (var p : points) { + int x = p[0]; + int y = p[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxRectangleArea(vector>& points) { + auto check = [&](int x1, int y1, int x2, int y2) -> bool { + int cnt = 0; + for (const auto& point : points) { + int x = point[0]; + int y = point[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + }; + + int ans = -1; + for (int i = 0; i < points.size(); i++) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; j++) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = min(x1, x2), y3 = min(y1, y2); + int x4 = max(x1, x2), y4 = max(y1, y2); + if (check(x3, y3, x4, y4)) { + ans = max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } +}; ``` #### Go ```go +func maxRectangleArea(points [][]int) int { + check := func(x1, y1, x2, y2 int) bool { + cnt := 0 + for _, point := range points { + x, y := point[0], point[1] + if x < x1 || x > x2 || y < y1 || y > y2 { + continue + } + if (x == x1 || x == x2) && (y == y1 || y == y2) { + cnt++ + continue + } + return false + } + return cnt == 4 + } + + ans := -1 + for i := 0; i < len(points); i++ { + x1, y1 := points[i][0], points[i][1] + for j := 0; j < i; j++ { + x2, y2 := points[j][0], points[j][1] + x3, y3 := min(x1, x2), min(y1, y2) + x4, y4 := max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4) { + ans = max(ans, (x4-x3)*(y4-y3)) + } + } + } + return ans +} +``` +#### TypeScript + +```ts +function maxRectangleArea(points: number[][]): number { + const check = (x1: number, y1: number, x2: number, y2: number): boolean => { + let cnt = 0; + for (const point of points) { + const [x, y] = point; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x === x1 || x === x2) && (y === y1 || y === y2)) { + cnt++; + continue; + } + return false; + } + return cnt === 4; + }; + + let ans = -1; + for (let i = 0; i < points.length; i++) { + const [x1, y1] = points[i]; + for (let j = 0; j < i; j++) { + const [x2, y2] = points[j]; + const [x3, y3] = [Math.min(x1, x2), Math.min(y1, y2)]; + const [x4, y4] = [Math.max(x1, x2), Math.max(y1, y2)]; + if (check(x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; +} ``` diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README_EN.md b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README_EN.md index 6b8a9dad06f92..2201cadb623dc 100644 --- a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README_EN.md +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README_EN.md @@ -93,32 +93,189 @@ tags: -### Solution 1 +### Solution 1: Enumeration + +We can enumerate the bottom-left corner $(x_3, y_3)$ and the top-right corner $(x_4, y_4)$ of the rectangle. Then, we enumerate all points $(x, y)$ and check if the point is inside or on the boundary of the rectangle. If it is, it does not meet the condition. Otherwise, we exclude the points outside the rectangle and check if there are 4 remaining points. If there are, these 4 points can form a rectangle. We calculate the area of the rectangle and take the maximum value. + +The time complexity is $O(n^3)$, where $n$ is the length of the array $\textit{points}$. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def maxRectangleArea(self, points: List[List[int]]) -> int: + def check(x1: int, y1: int, x2: int, y2: int) -> bool: + cnt = 0 + for x, y in points: + if x < x1 or x > x2 or y < y1 or y > y2: + continue + if (x == x1 or x == x2) and (y == y1 or y == y2): + cnt += 1 + continue + return False + return cnt == 4 + + ans = -1 + for i, (x1, y1) in enumerate(points): + for x2, y2 in points[:i]: + x3, y3 = min(x1, x2), min(y1, y2) + x4, y4 = max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4): + ans = max(ans, (x4 - x3) * (y4 - y3)) + return ans ``` #### Java ```java - +class Solution { + public int maxRectangleArea(int[][] points) { + int ans = -1; + for (int i = 0; i < points.length; ++i) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; ++j) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = Math.min(x1, x2), y3 = Math.min(y1, y2); + int x4 = Math.max(x1, x2), y4 = Math.max(y1, y2); + if (check(points, x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } + + private boolean check(int[][] points, int x1, int y1, int x2, int y2) { + int cnt = 0; + for (var p : points) { + int x = p[0]; + int y = p[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxRectangleArea(vector>& points) { + auto check = [&](int x1, int y1, int x2, int y2) -> bool { + int cnt = 0; + for (const auto& point : points) { + int x = point[0]; + int y = point[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + }; + + int ans = -1; + for (int i = 0; i < points.size(); i++) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; j++) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = min(x1, x2), y3 = min(y1, y2); + int x4 = max(x1, x2), y4 = max(y1, y2); + if (check(x3, y3, x4, y4)) { + ans = max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } +}; ``` #### Go ```go +func maxRectangleArea(points [][]int) int { + check := func(x1, y1, x2, y2 int) bool { + cnt := 0 + for _, point := range points { + x, y := point[0], point[1] + if x < x1 || x > x2 || y < y1 || y > y2 { + continue + } + if (x == x1 || x == x2) && (y == y1 || y == y2) { + cnt++ + continue + } + return false + } + return cnt == 4 + } + + ans := -1 + for i := 0; i < len(points); i++ { + x1, y1 := points[i][0], points[i][1] + for j := 0; j < i; j++ { + x2, y2 := points[j][0], points[j][1] + x3, y3 := min(x1, x2), min(y1, y2) + x4, y4 := max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4) { + ans = max(ans, (x4-x3)*(y4-y3)) + } + } + } + return ans +} +``` +#### TypeScript + +```ts +function maxRectangleArea(points: number[][]): number { + const check = (x1: number, y1: number, x2: number, y2: number): boolean => { + let cnt = 0; + for (const point of points) { + const [x, y] = point; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x === x1 || x === x2) && (y === y1 || y === y2)) { + cnt++; + continue; + } + return false; + } + return cnt === 4; + }; + + let ans = -1; + for (let i = 0; i < points.length; i++) { + const [x1, y1] = points[i]; + for (let j = 0; j < i; j++) { + const [x2, y2] = points[j]; + const [x3, y3] = [Math.min(x1, x2), Math.min(y1, y2)]; + const [x4, y4] = [Math.max(x1, x2), Math.max(y1, y2)]; + if (check(x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; +} ``` diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.cpp b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.cpp new file mode 100644 index 0000000000000..aed4d579cc4a0 --- /dev/null +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int maxRectangleArea(vector>& points) { + auto check = [&](int x1, int y1, int x2, int y2) -> bool { + int cnt = 0; + for (const auto& point : points) { + int x = point[0]; + int y = point[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + }; + + int ans = -1; + for (int i = 0; i < points.size(); i++) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; j++) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = min(x1, x2), y3 = min(y1, y2); + int x4 = max(x1, x2), y4 = max(y1, y2); + if (check(x3, y3, x4, y4)) { + ans = max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } +}; diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.go b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.go new file mode 100644 index 0000000000000..03ccde2a8bd2b --- /dev/null +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.go @@ -0,0 +1,31 @@ +func maxRectangleArea(points [][]int) int { + check := func(x1, y1, x2, y2 int) bool { + cnt := 0 + for _, point := range points { + x, y := point[0], point[1] + if x < x1 || x > x2 || y < y1 || y > y2 { + continue + } + if (x == x1 || x == x2) && (y == y1 || y == y2) { + cnt++ + continue + } + return false + } + return cnt == 4 + } + + ans := -1 + for i := 0; i < len(points); i++ { + x1, y1 := points[i][0], points[i][1] + for j := 0; j < i; j++ { + x2, y2 := points[j][0], points[j][1] + x3, y3 := min(x1, x2), min(y1, y2) + x4, y4 := max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4) { + ans = max(ans, (x4-x3)*(y4-y3)) + } + } + } + return ans +} diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.java b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.java new file mode 100644 index 0000000000000..471f5c110eb6b --- /dev/null +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.java @@ -0,0 +1,34 @@ +class Solution { + public int maxRectangleArea(int[][] points) { + int ans = -1; + for (int i = 0; i < points.length; ++i) { + int x1 = points[i][0], y1 = points[i][1]; + for (int j = 0; j < i; ++j) { + int x2 = points[j][0], y2 = points[j][1]; + int x3 = Math.min(x1, x2), y3 = Math.min(y1, y2); + int x4 = Math.max(x1, x2), y4 = Math.max(y1, y2); + if (check(points, x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; + } + + private boolean check(int[][] points, int x1, int y1, int x2, int y2) { + int cnt = 0; + for (var p : points) { + int x = p[0]; + int y = p[1]; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x == x1 || x == x2) && (y == y1 || y == y2)) { + cnt++; + continue; + } + return false; + } + return cnt == 4; + } +} diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.py b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.py new file mode 100644 index 0000000000000..2f15a12ae7877 --- /dev/null +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def maxRectangleArea(self, points: List[List[int]]) -> int: + def check(x1: int, y1: int, x2: int, y2: int) -> bool: + cnt = 0 + for x, y in points: + if x < x1 or x > x2 or y < y1 or y > y2: + continue + if (x == x1 or x == x2) and (y == y1 or y == y2): + cnt += 1 + continue + return False + return cnt == 4 + + ans = -1 + for i, (x1, y1) in enumerate(points): + for x2, y2 in points[:i]: + x3, y3 = min(x1, x2), min(y1, y2) + x4, y4 = max(x1, x2), max(y1, y2) + if check(x3, y3, x4, y4): + ans = max(ans, (x4 - x3) * (y4 - y3)) + return ans diff --git a/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.ts b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.ts new file mode 100644 index 0000000000000..170e2a4d1df1b --- /dev/null +++ b/solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/Solution.ts @@ -0,0 +1,31 @@ +function maxRectangleArea(points: number[][]): number { + const check = (x1: number, y1: number, x2: number, y2: number): boolean => { + let cnt = 0; + for (const point of points) { + const [x, y] = point; + if (x < x1 || x > x2 || y < y1 || y > y2) { + continue; + } + if ((x === x1 || x === x2) && (y === y1 || y === y2)) { + cnt++; + continue; + } + return false; + } + return cnt === 4; + }; + + let ans = -1; + for (let i = 0; i < points.length; i++) { + const [x1, y1] = points[i]; + for (let j = 0; j < i; j++) { + const [x2, y2] = points[j]; + const [x3, y3] = [Math.min(x1, x2), Math.min(y1, y2)]; + const [x4, y4] = [Math.max(x1, x2), Math.max(y1, y2)]; + if (check(x3, y3, x4, y4)) { + ans = Math.max(ans, (x4 - x3) * (y4 - y3)); + } + } + } + return ans; +}