Skip to content

[pull] main from doocs:main #210

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 4 commits into from
Apr 1, 2023
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
50 changes: 49 additions & 1 deletion solution/0800-0899/0836.Rectangle Overlap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,70 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:判断不重叠的情况**

我们记矩形 $rec1$ 的坐标点为 $(x_1, y_1, x_2, y_2)$,矩形 $rec2$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。

那么当满足以下任一条件时,矩形 $rec1$ 和 $rec2$ 不重叠:

- 满足 $y_3 \geq y_2$,即 $rec2$ 在 $rec1$ 的上方;
- 满足 $y_4 \leq y_1$,即 $rec2$ 在 $rec1$ 的下方;
- 满足 $x_3 \geq x_2$,即 $rec2$ 在 $rec1$ 的右方;
- 满足 $x_4 \leq x_1$,即 $rec2$ 在 $rec1$ 的左方。

当以上条件都不满足时,矩形 $rec1$ 和 $rec2$ 重叠。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
x1, y1, x2, y2 = rec1
x3, y3, x4, y4 = rec2
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
}
```

### **C++**

```cpp
class Solution {
public:
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
};
```

### **Go**

```go
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
}
```

### **...**
Expand Down
35 changes: 34 additions & 1 deletion solution/0800-0899/0836.Rectangle Overlap/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,46 @@
### **Python3**

```python

class Solution:
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
x1, y1, x2, y2 = rec1
x3, y3, x4, y4 = rec2
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)
```

### **Java**

```java
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
}
```

### **C++**

```cpp
class Solution {
public:
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
};
```

### **Go**

```go
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
}
```

### **...**
Expand Down
12 changes: 3 additions & 9 deletions solution/0800-0899/0836.Rectangle Overlap/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
class Solution {
public:
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
int l1 = rec1[1], u1 = rec1[0], r1 = rec1[3], d1 = rec1[2];
int l2 = rec2[1], u2 = rec2[0], r2 = rec2[3], d2 = rec2[2];

//printf("1: (%d,%d), (%d,%d)\n", l1, u1, r1, d1) ;
//printf("2: (%d,%d), (%d,%d)\n", l2, u2, r2, d2) ;

if (l1 < r2 && u1 < d2 && l2 < r1 && u2 < d1)
return true;
return false;
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
};
5 changes: 5 additions & 0 deletions solution/0800-0899/0836.Rectangle Overlap/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
}
7 changes: 7 additions & 0 deletions solution/0800-0899/0836.Rectangle Overlap/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
}
}
5 changes: 5 additions & 0 deletions solution/0800-0899/0836.Rectangle Overlap/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
x1, y1, x2, y2 = rec1
x3, y3, x4, y4 = rec2
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)
8 changes: 7 additions & 1 deletion solution/0800-0899/0844.Backspace String Compare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@

**方法一:双指针**

时间复杂度 O(len(s) + len(t)),空间复杂度 O(1)。
我们用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和 $t$ 的末尾。

每次向前移动一个字符,如果当前字符是退格符,则跳过当前字符,同时退格符的数量加一,如果当前字符不是退格符,则判断退格符的数量,如果退格符的数量大于 $0$,则跳过当前字符,同时退格符的数量减一,如果退格符的数量等于 $0$,那么该字符需要进行比较。

我们每次找到两个字符串中需要比较的字符,然后进行比较,如果两个字符不相等,则返回 $false$,如果遍历完两个字符串,都没有发现不相等的字符,则返回 $true$。

时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是字符串 $s$ 和 $t$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:BFS(A\* 算法)**

因为每条边权值一样,所以用 BFS 就能得出最短路径,过程中可以用**状态压缩**记录节点的访问情况。另外,同一个节点 u 以及对应的节点访问情况需要保证只被搜索过一次,因此可以用 `vis(u, state)` 表示是否已经被搜索过,防止无效的重复搜索。

本题也属于 BFS 最小步数模型,可以使用 A\* 算法优化搜索。
Expand Down
131 changes: 84 additions & 47 deletions solution/0800-0899/0851.Loud and Rich/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ answer[7] = 7,

<!-- 这里可写通用的实现逻辑 -->

根据 richer 关系构建有向图,如果 a 比 b 有钱,那么连一条从 b 到 a 的有向边,最终构建出一个有向无环图。
**方法一:DFS**

我们知道,从图的任一点 i 出发,沿着有向边所能访问到的点,都比 i 更有钱。DFS 深搜即可。
我们先用邻接表 $g$ 存储 $richer$ 数组中的信息,其中 $g[i]$ 表示所有比 $i$ 更有钱的人的集合。

然后对于每个人 $i$,我们用 DFS 遍历所有比 $i$ 更有钱的人,找到其中安静值最小的人,即为答案。

时间复杂度 $O(m + n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为 $richer$ 数组和 $quiet$ 数组的长度。

<!-- tabs:start -->

Expand All @@ -70,13 +74,7 @@ answer[7] = 7,
```python
class Solution:
def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
n = len(quiet)
g = defaultdict(list)
for a, b in richer:
g[b].append(a)
ans = [-1] * n

def dfs(i):
def dfs(i: int):
if ans[i] != -1:
return
ans[i] = i
Expand All @@ -85,6 +83,11 @@ class Solution:
if quiet[ans[j]] < quiet[ans[i]]:
ans[i] = ans[j]

g = defaultdict(list)
for a, b in richer:
g[b].append(a)
n = len(quiet)
ans = [-1] * n
for i in range(n):
dfs(i)
return ans
Expand All @@ -96,19 +99,22 @@ class Solution:

```java
class Solution {
private Map<Integer, List<Integer>> g;
private List<Integer>[] g;
private int n;
private int[] quiet;
private int[] ans;

public int[] loudAndRich(int[][] richer, int[] quiet) {
g = new HashMap<>();
n = quiet.length;
this.quiet = quiet;
ans = new int[quiet.length];
g = new List[n];
ans = new int[n];
Arrays.fill(ans, -1);
for (int[] r : richer) {
g.computeIfAbsent(r[1], k -> new ArrayList<>()).add(r[0]);
Arrays.setAll(g, k -> new ArrayList<>());
for (var r : richer) {
g[r[1]].add(r[0]);
}
for (int i = 0; i < quiet.length; ++i) {
for (int i = 0; i < n; ++i) {
dfs(i);
}
return ans;
Expand All @@ -119,10 +125,7 @@ class Solution {
return;
}
ans[i] = i;
if (!g.containsKey(i)) {
return;
}
for (int j : g.get(i)) {
for (int j : g[i]) {
dfs(j);
if (quiet[ans[j]] < quiet[ans[i]]) {
ans[i] = ans[j];
Expand All @@ -140,18 +143,25 @@ public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<vector<int>> g(n);
for (auto& r : richer) g[r[1]].push_back(r[0]);
for (auto& r : richer) {
g[r[1]].push_back(r[0]);
}
vector<int> ans(n, -1);
function<void(int)> dfs = [&](int i) {
if (ans[i] != -1) return;
if (ans[i] != -1) {
return;
}
ans[i] = i;
for (int j : g[i]) {
dfs(j);
if (quiet[ans[j]] < quiet[ans[i]]) ans[i] = ans[j];
if (quiet[ans[j]] < quiet[ans[i]]) {
ans[i] = ans[j];
}
}
};
for (int i = 0; i < n; ++i)
for (int i = 0; i < n; ++i) {
dfs(i);
}
return ans;
}
};
Expand All @@ -161,35 +171,62 @@ public:

```go
func loudAndRich(richer [][]int, quiet []int) []int {
n := len(quiet)
ans := make([]int, n)
g := make([][]int, n)
for i := 0; i < n; i++ {
ans[i] = -1
g[i] = make([]int, 0)
}
for _, r := range richer {
g[r[1]] = append(g[r[1]], r[0])
}
n := len(quiet)
g := make([][]int, n)
ans := make([]int, n)
for i := range g {
ans[i] = -1
}
for _, r := range richer {
a, b := r[0], r[1]
g[b] = append(g[b], a)
}
var dfs func(int)
dfs = func(i int) {
if ans[i] != -1 {
return
}
ans[i] = i
for _, j := range g[i] {
dfs(j)
if quiet[ans[j]] < quiet[ans[i]] {
ans[i] = ans[j]
}
}
}
for i := range ans {
dfs(i)
}
return ans
}
```

var dfs func(i int)
dfs = func(i int) {
if ans[i] != - 1 {
return
### **TypeScript**

```ts
function loudAndRich(richer: number[][], quiet: number[]): number[] {
const n = quiet.length;
const g: number[][] = new Array(n).fill(0).map(() => []);
for (const [a, b] of richer) {
g[b].push(a);
}
const ans: number[] = new Array(n).fill(-1);
const dfs = (i: number) => {
if (ans[i] != -1) {
return ans;
}
ans[i] = i
for _, j := range g[i] {
dfs(j)
if quiet[ans[j]] < quiet[ans[i]] {
ans[i] = ans[j]
ans[i] = i;
for (const j of g[i]) {
dfs(j);
if (quiet[ans[j]] < quiet[ans[i]]) {
ans[i] = ans[j];
}
}
};
for (let i = 0; i < n; ++i) {
dfs(i);
}

for i := 0; i < n; i++ {
dfs(i)
}
return ans
return ans;
}
```

Expand Down
Loading