Skip to content

[pull] main from doocs:main #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3375.Minimum%20Operations%20to%20Make%20Array%20Values%20Equal%20to%20K/README.md
tags:
- 数组
- 哈希表
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3375.Minimum%20Operations%20to%20Make%20Array%20Values%20Equal%20to%20K/README_EN.md
tags:
- Array
- Hash Table
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3376.Minimum%20Time%20to%20Break%20Locks%20I/README.md
tags:
- 位运算
- 数组
- 动态规划
- 回溯
- 状态压缩
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3376.Minimum%20Time%20to%20Break%20Locks%20I/README_EN.md
tags:
- Bit Manipulation
- Array
- Dynamic Programming
- Backtracking
- Bitmask
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3377.Digit%20Operations%20to%20Make%20Two%20Integers%20Equal/README.md
tags:
- 图
- 数学
- 数论
- 最短路
- 堆(优先队列)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3377.Digit%20Operations%20to%20Make%20Two%20Integers%20Equal/README_EN.md
tags:
- Graph
- Math
- Number Theory
- Shortest Path
- Heap (Priority Queue)
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3378.Count%20Connected%20Components%20in%20LCM%20Graph/README.md
tags:
- 并查集
- 数组
- 哈希表
- 数学
- 数论
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3378.Count%20Connected%20Components%20in%20LCM%20Graph/README_EN.md
tags:
- Union Find
- Array
- Hash Table
- Math
- Number Theory
---

<!-- problem:start -->
Expand Down
3 changes: 3 additions & 0 deletions solution/3300-3399/3379.Transformed Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3379.Transformed%20Array/README.md
tags:
- 数组
- 模拟
---

<!-- problem:start -->
Expand Down
3 changes: 3 additions & 0 deletions solution/3300-3399/3379.Transformed Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3379.Transformed%20Array/README_EN.md
tags:
- Array
- Simulation
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3380.Maximum%20Area%20Rectangle%20With%20Point%20Constraints%20I/README.md
tags:
- 树状数组
- 线段树
- 几何
- 数组
- 数学
- 枚举
- 排序
---

<!-- problem:start -->
Expand Down Expand Up @@ -87,32 +95,189 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3380.Ma

<!-- solution:start -->

### 方法一
### 方法一:枚举

我们可以枚举矩形的左下角下标 $(x_3, y_3)$ 和右上角下标 $(x_4, y_4)$,然后枚举所有的点 $(x, y)$,判断点是否在矩形的内部或边界上,如果是,说明不满足条件,否则,我们排除掉在矩形外部的点,然后判断剩下的点是否有 4 个,如果有,说明这 4 个点可以构成一个矩形,计算矩形的面积,取最大值即可。

时间复杂度 $O(n^3)$,其中 $n$ 是数组 $\textit{points}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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<vector<int>>& 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;
}
```

<!-- tabs:end -->
Expand Down
Loading