Skip to content

Commit 1d594e2

Browse files
authored
feat: add solutions to lc problem: No.3380 (#3852)
No.3380.Maximum Area Rectangle With Point Constraints I
1 parent 2038cfd commit 1d594e2

File tree

7 files changed

+474
-8
lines changed

7 files changed

+474
-8
lines changed

solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README.md

+161-4
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,189 @@ tags:
9595

9696
<!-- solution:start -->
9797

98-
### 方法一
98+
### 方法一:枚举
99+
100+
我们可以枚举矩形的左下角下标 $(x_3, y_3)$ 和右上角下标 $(x_4, y_4)$,然后枚举所有的点 $(x, y)$,判断点是否在矩形的内部或边界上,如果是,说明不满足条件,否则,我们排除掉在矩形外部的点,然后判断剩下的点是否有 4 个,如果有,说明这 4 个点可以构成一个矩形,计算矩形的面积,取最大值即可。
101+
102+
时间复杂度 $O(n^3)$,其中 $n$ 是数组 $\textit{points}$ 的长度。空间复杂度 $O(1)$。
99103

100104
<!-- tabs:start -->
101105

102106
#### Python3
103107

104108
```python
105-
109+
class Solution:
110+
def maxRectangleArea(self, points: List[List[int]]) -> int:
111+
def check(x1: int, y1: int, x2: int, y2: int) -> bool:
112+
cnt = 0
113+
for x, y in points:
114+
if x < x1 or x > x2 or y < y1 or y > y2:
115+
continue
116+
if (x == x1 or x == x2) and (y == y1 or y == y2):
117+
cnt += 1
118+
continue
119+
return False
120+
return cnt == 4
121+
122+
ans = -1
123+
for i, (x1, y1) in enumerate(points):
124+
for x2, y2 in points[:i]:
125+
x3, y3 = min(x1, x2), min(y1, y2)
126+
x4, y4 = max(x1, x2), max(y1, y2)
127+
if check(x3, y3, x4, y4):
128+
ans = max(ans, (x4 - x3) * (y4 - y3))
129+
return ans
106130
```
107131

108132
#### Java
109133

110134
```java
111-
135+
class Solution {
136+
public int maxRectangleArea(int[][] points) {
137+
int ans = -1;
138+
for (int i = 0; i < points.length; ++i) {
139+
int x1 = points[i][0], y1 = points[i][1];
140+
for (int j = 0; j < i; ++j) {
141+
int x2 = points[j][0], y2 = points[j][1];
142+
int x3 = Math.min(x1, x2), y3 = Math.min(y1, y2);
143+
int x4 = Math.max(x1, x2), y4 = Math.max(y1, y2);
144+
if (check(points, x3, y3, x4, y4)) {
145+
ans = Math.max(ans, (x4 - x3) * (y4 - y3));
146+
}
147+
}
148+
}
149+
return ans;
150+
}
151+
152+
private boolean check(int[][] points, int x1, int y1, int x2, int y2) {
153+
int cnt = 0;
154+
for (var p : points) {
155+
int x = p[0];
156+
int y = p[1];
157+
if (x < x1 || x > x2 || y < y1 || y > y2) {
158+
continue;
159+
}
160+
if ((x == x1 || x == x2) && (y == y1 || y == y2)) {
161+
cnt++;
162+
continue;
163+
}
164+
return false;
165+
}
166+
return cnt == 4;
167+
}
168+
}
112169
```
113170

114171
#### C++
115172

116173
```cpp
117-
174+
class Solution {
175+
public:
176+
int maxRectangleArea(vector<vector<int>>& points) {
177+
auto check = [&](int x1, int y1, int x2, int y2) -> bool {
178+
int cnt = 0;
179+
for (const auto& point : points) {
180+
int x = point[0];
181+
int y = point[1];
182+
if (x < x1 || x > x2 || y < y1 || y > y2) {
183+
continue;
184+
}
185+
if ((x == x1 || x == x2) && (y == y1 || y == y2)) {
186+
cnt++;
187+
continue;
188+
}
189+
return false;
190+
}
191+
return cnt == 4;
192+
};
193+
194+
int ans = -1;
195+
for (int i = 0; i < points.size(); i++) {
196+
int x1 = points[i][0], y1 = points[i][1];
197+
for (int j = 0; j < i; j++) {
198+
int x2 = points[j][0], y2 = points[j][1];
199+
int x3 = min(x1, x2), y3 = min(y1, y2);
200+
int x4 = max(x1, x2), y4 = max(y1, y2);
201+
if (check(x3, y3, x4, y4)) {
202+
ans = max(ans, (x4 - x3) * (y4 - y3));
203+
}
204+
}
205+
}
206+
return ans;
207+
}
208+
};
118209
```
119210

120211
#### Go
121212

122213
```go
214+
func maxRectangleArea(points [][]int) int {
215+
check := func(x1, y1, x2, y2 int) bool {
216+
cnt := 0
217+
for _, point := range points {
218+
x, y := point[0], point[1]
219+
if x < x1 || x > x2 || y < y1 || y > y2 {
220+
continue
221+
}
222+
if (x == x1 || x == x2) && (y == y1 || y == y2) {
223+
cnt++
224+
continue
225+
}
226+
return false
227+
}
228+
return cnt == 4
229+
}
230+
231+
ans := -1
232+
for i := 0; i < len(points); i++ {
233+
x1, y1 := points[i][0], points[i][1]
234+
for j := 0; j < i; j++ {
235+
x2, y2 := points[j][0], points[j][1]
236+
x3, y3 := min(x1, x2), min(y1, y2)
237+
x4, y4 := max(x1, x2), max(y1, y2)
238+
if check(x3, y3, x4, y4) {
239+
ans = max(ans, (x4-x3)*(y4-y3))
240+
}
241+
}
242+
}
243+
return ans
244+
}
245+
```
123246

247+
#### TypeScript
248+
249+
```ts
250+
function maxRectangleArea(points: number[][]): number {
251+
const check = (x1: number, y1: number, x2: number, y2: number): boolean => {
252+
let cnt = 0;
253+
for (const point of points) {
254+
const [x, y] = point;
255+
if (x < x1 || x > x2 || y < y1 || y > y2) {
256+
continue;
257+
}
258+
if ((x === x1 || x === x2) && (y === y1 || y === y2)) {
259+
cnt++;
260+
continue;
261+
}
262+
return false;
263+
}
264+
return cnt === 4;
265+
};
266+
267+
let ans = -1;
268+
for (let i = 0; i < points.length; i++) {
269+
const [x1, y1] = points[i];
270+
for (let j = 0; j < i; j++) {
271+
const [x2, y2] = points[j];
272+
const [x3, y3] = [Math.min(x1, x2), Math.min(y1, y2)];
273+
const [x4, y4] = [Math.max(x1, x2), Math.max(y1, y2)];
274+
if (check(x3, y3, x4, y4)) {
275+
ans = Math.max(ans, (x4 - x3) * (y4 - y3));
276+
}
277+
}
278+
}
279+
return ans;
280+
}
124281
```
125282

126283
<!-- tabs:end -->

solution/3300-3399/3380.Maximum Area Rectangle With Point Constraints I/README_EN.md

+161-4
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,189 @@ tags:
9393

9494
<!-- solution:start -->
9595

96-
### Solution 1
96+
### Solution 1: Enumeration
97+
98+
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.
99+
100+
The time complexity is $O(n^3)$, where $n$ is the length of the array $\textit{points}$. The space complexity is $O(1)$.
97101

98102
<!-- tabs:start -->
99103

100104
#### Python3
101105

102106
```python
103-
107+
class Solution:
108+
def maxRectangleArea(self, points: List[List[int]]) -> int:
109+
def check(x1: int, y1: int, x2: int, y2: int) -> bool:
110+
cnt = 0
111+
for x, y in points:
112+
if x < x1 or x > x2 or y < y1 or y > y2:
113+
continue
114+
if (x == x1 or x == x2) and (y == y1 or y == y2):
115+
cnt += 1
116+
continue
117+
return False
118+
return cnt == 4
119+
120+
ans = -1
121+
for i, (x1, y1) in enumerate(points):
122+
for x2, y2 in points[:i]:
123+
x3, y3 = min(x1, x2), min(y1, y2)
124+
x4, y4 = max(x1, x2), max(y1, y2)
125+
if check(x3, y3, x4, y4):
126+
ans = max(ans, (x4 - x3) * (y4 - y3))
127+
return ans
104128
```
105129

106130
#### Java
107131

108132
```java
109-
133+
class Solution {
134+
public int maxRectangleArea(int[][] points) {
135+
int ans = -1;
136+
for (int i = 0; i < points.length; ++i) {
137+
int x1 = points[i][0], y1 = points[i][1];
138+
for (int j = 0; j < i; ++j) {
139+
int x2 = points[j][0], y2 = points[j][1];
140+
int x3 = Math.min(x1, x2), y3 = Math.min(y1, y2);
141+
int x4 = Math.max(x1, x2), y4 = Math.max(y1, y2);
142+
if (check(points, x3, y3, x4, y4)) {
143+
ans = Math.max(ans, (x4 - x3) * (y4 - y3));
144+
}
145+
}
146+
}
147+
return ans;
148+
}
149+
150+
private boolean check(int[][] points, int x1, int y1, int x2, int y2) {
151+
int cnt = 0;
152+
for (var p : points) {
153+
int x = p[0];
154+
int y = p[1];
155+
if (x < x1 || x > x2 || y < y1 || y > y2) {
156+
continue;
157+
}
158+
if ((x == x1 || x == x2) && (y == y1 || y == y2)) {
159+
cnt++;
160+
continue;
161+
}
162+
return false;
163+
}
164+
return cnt == 4;
165+
}
166+
}
110167
```
111168

112169
#### C++
113170

114171
```cpp
115-
172+
class Solution {
173+
public:
174+
int maxRectangleArea(vector<vector<int>>& points) {
175+
auto check = [&](int x1, int y1, int x2, int y2) -> bool {
176+
int cnt = 0;
177+
for (const auto& point : points) {
178+
int x = point[0];
179+
int y = point[1];
180+
if (x < x1 || x > x2 || y < y1 || y > y2) {
181+
continue;
182+
}
183+
if ((x == x1 || x == x2) && (y == y1 || y == y2)) {
184+
cnt++;
185+
continue;
186+
}
187+
return false;
188+
}
189+
return cnt == 4;
190+
};
191+
192+
int ans = -1;
193+
for (int i = 0; i < points.size(); i++) {
194+
int x1 = points[i][0], y1 = points[i][1];
195+
for (int j = 0; j < i; j++) {
196+
int x2 = points[j][0], y2 = points[j][1];
197+
int x3 = min(x1, x2), y3 = min(y1, y2);
198+
int x4 = max(x1, x2), y4 = max(y1, y2);
199+
if (check(x3, y3, x4, y4)) {
200+
ans = max(ans, (x4 - x3) * (y4 - y3));
201+
}
202+
}
203+
}
204+
return ans;
205+
}
206+
};
116207
```
117208

118209
#### Go
119210

120211
```go
212+
func maxRectangleArea(points [][]int) int {
213+
check := func(x1, y1, x2, y2 int) bool {
214+
cnt := 0
215+
for _, point := range points {
216+
x, y := point[0], point[1]
217+
if x < x1 || x > x2 || y < y1 || y > y2 {
218+
continue
219+
}
220+
if (x == x1 || x == x2) && (y == y1 || y == y2) {
221+
cnt++
222+
continue
223+
}
224+
return false
225+
}
226+
return cnt == 4
227+
}
228+
229+
ans := -1
230+
for i := 0; i < len(points); i++ {
231+
x1, y1 := points[i][0], points[i][1]
232+
for j := 0; j < i; j++ {
233+
x2, y2 := points[j][0], points[j][1]
234+
x3, y3 := min(x1, x2), min(y1, y2)
235+
x4, y4 := max(x1, x2), max(y1, y2)
236+
if check(x3, y3, x4, y4) {
237+
ans = max(ans, (x4-x3)*(y4-y3))
238+
}
239+
}
240+
}
241+
return ans
242+
}
243+
```
121244

245+
#### TypeScript
246+
247+
```ts
248+
function maxRectangleArea(points: number[][]): number {
249+
const check = (x1: number, y1: number, x2: number, y2: number): boolean => {
250+
let cnt = 0;
251+
for (const point of points) {
252+
const [x, y] = point;
253+
if (x < x1 || x > x2 || y < y1 || y > y2) {
254+
continue;
255+
}
256+
if ((x === x1 || x === x2) && (y === y1 || y === y2)) {
257+
cnt++;
258+
continue;
259+
}
260+
return false;
261+
}
262+
return cnt === 4;
263+
};
264+
265+
let ans = -1;
266+
for (let i = 0; i < points.length; i++) {
267+
const [x1, y1] = points[i];
268+
for (let j = 0; j < i; j++) {
269+
const [x2, y2] = points[j];
270+
const [x3, y3] = [Math.min(x1, x2), Math.min(y1, y2)];
271+
const [x4, y4] = [Math.max(x1, x2), Math.max(y1, y2)];
272+
if (check(x3, y3, x4, y4)) {
273+
ans = Math.max(ans, (x4 - x3) * (y4 - y3));
274+
}
275+
}
276+
}
277+
return ans;
278+
}
122279
```
123280

124281
<!-- tabs:end -->

0 commit comments

Comments
 (0)