Skip to content
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

feat: add solutions to lc problem: No.2200 #2091

Merged
merged 1 commit into from
Dec 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

**方法一:枚举**

我们在 $[0, n)$ 的范围内枚举下标 $i$,对于每个下标 $i$,我们在 $[0, n)$ 的范围内枚举下标 $j$,如果 $|i - j| \leq k$ 且 $nums[j] == key$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中,然后跳出内层循环,枚举下一个下标 $i$。
我们在 $[0, n)$ 的范围内枚举下标 $i$,对于每个下标 $i$,我们在 $[0, n)$ 的范围内枚举下标 $j$,如果 $|i - j| \leq k$ 且 $nums[j] = key$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中,然后跳出内层循环,枚举下一个下标 $i$。

时间复杂度 $O(n^2)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Hence, we return [0,1,2,3,4].

**Solution 1: Enumeration**

We enumerate the index $i$ in the range $[0, n)$, and for each index $i$, we enumerate the index $j$ in the range $[0, n)$. If $|i - j| \leq k$ and $nums[j] == key$, then $i$ is a K-nearest neighbor index. We add $i$ to the answer array, then break the inner loop and enumerate the next index $i$.
We enumerate the index $i$ in the range $[0, n)$, and for each index $i$, we enumerate the index $j$ in the range $[0, n)$. If $|i - j| \leq k$ and $nums[j] = key$, then $i$ is a K-nearest neighbor index. We add $i$ to the answer array, then break the inner loop and enumerate the next index $i$.

The time complexity is $O(n^2)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

Expand Down
190 changes: 116 additions & 74 deletions solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@

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

**方法一:哈希表**

我们可以用哈希表 $s$ 记录所有挖掘的单元格,然后遍历所有工件,判断工件的所有部分是否都在哈希表中,若是则可以提取该工件,答案加一。

时间复杂度 $O(m + k)$,空间复杂度 $O(k)$,其中 $m$ 是工件的数量,而 $k$ 是挖掘的单元格的数量。

<!-- tabs:start -->

### **Python3**
Expand All @@ -77,16 +83,14 @@ class Solution:
def digArtifacts(
self, n: int, artifacts: List[List[int]], dig: List[List[int]]
) -> int:
def check(artifact):
r1, c1, r2, c2 = artifact
for x in range(r1, r2 + 1):
for y in range(c1, c2 + 1):
if (x, y) not in s:
return False
return True
def check(a: List[int]) -> bool:
x1, y1, x2, y2 = a
return all(
(x, y) in s for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)
)

s = {(i, j) for i, j in dig}
return sum(check(v) for v in artifacts)
return sum(check(a) for a in artifacts)
```

### **Java**
Expand All @@ -95,55 +99,32 @@ class Solution:

```java
class Solution {
private Set<Integer> s = new HashSet<>();
private int n;

public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
Set<Integer> s = new HashSet<>();
for (int[] d : dig) {
s.add(d[0] * n + d[1]);
this.n = n;
for (var p : dig) {
s.add(p[0] * n + p[1]);
}
int ans = 0;
for (int[] a : artifacts) {
if (check(a, s, n)) {
++ans;
}
for (var a : artifacts) {
ans += check(a);
}
return ans;
}

private boolean check(int[] a, Set<Integer> s, int n) {
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
for (int i = r1; i <= r2; ++i) {
for (int j = c1; j <= c2; ++j) {
if (!s.contains(i * n + j)) {
return false;
}
}
}
return true;
}
}
```

### **TypeScript**

```ts
function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {
let visited = Array.from({ length: n }, v => new Array(n).fill(false));
for (let [i, j] of dig) {
visited[i][j] = true;
}
let ans = 0;
for (let [a, b, c, d] of artifacts) {
let flag = true;
for (let i = a; i <= c && flag; i++) {
for (let j = b; j <= d && flag; j++) {
if (!visited[i][j]) {
flag = false;
private int check(int[] a) {
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
if (!s.contains(x * n + y)) {
return 0;
}
}
}
flag && ans++;
return 1;
}
return ans;
}
```

Expand All @@ -154,52 +135,113 @@ class Solution {
public:
int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {
unordered_set<int> s;
for (auto& d : dig) s.insert(d[0] * n + d[1]);
int ans = 0;
for (auto& a : artifacts) ans += check(a, s, n);
return ans;
}

bool check(vector<int>& a, unordered_set<int>& s, int n) {
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
for (int i = r1; i <= r2; ++i) {
for (int j = c1; j <= c2; ++j) {
if (!s.count(i * n + j)) {
return false;
for (auto& p : dig) {
s.insert(p[0] * n + p[1]);
}
auto check = [&](vector<int>& a) {
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
if (!s.count(x * n + y)) {
return 0;
}
}
}
return 1;
};
int ans = 0;
for (auto& a : artifacts) {
ans += check(a);
}
return true;
return ans;
}
};
```

### **Go**

```go
func digArtifacts(n int, artifacts [][]int, dig [][]int) int {
func digArtifacts(n int, artifacts [][]int, dig [][]int) (ans int) {
s := map[int]bool{}
for _, d := range dig {
s[d[0]*n+d[1]] = true
for _, p := range dig {
s[p[0]*n+p[1]] = true
}
check := func(a []int) bool {
r1, c1, r2, c2 := a[0], a[1], a[2], a[3]
for i := r1; i <= r2; i++ {
for j := c1; j <= c2; j++ {
if !s[i*n+j] {
return false
check := func(a []int) int {
x1, y1, x2, y2 := a[0], a[1], a[2], a[3]
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
if !s[x*n+y] {
return 0
}
}
}
return true
return 1
}
ans := 0
for _, a := range artifacts {
if check(a) {
ans++
}
ans += check(a)
}
return ans
return
}
```

### **TypeScript**

```ts
function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {
const s: Set<number> = new Set();
for (const [x, y] of dig) {
s.add(x * n + y);
}
let ans = 0;
const check = (a: number[]): number => {
const [x1, y1, x2, y2] = a;
for (let x = x1; x <= x2; ++x) {
for (let y = y1; y <= y2; ++y) {
if (!s.has(x * n + y)) {
return 0;
}
}
}
return 1;
};
for (const a of artifacts) {
ans += check(a);
}
return ans;
}
```

### **Rust**

```rust
use std::collections::HashSet;

impl Solution {
pub fn dig_artifacts(n: i32, artifacts: Vec<Vec<i32>>, dig: Vec<Vec<i32>>) -> i32 {
let mut s: HashSet<i32> = HashSet::new();
for p in dig {
s.insert(p[0] * n + p[1]);
}
let check = |a: &[i32]| -> i32 {
let x1 = a[0];
let y1 = a[1];
let x2 = a[2];
let y2 = a[3];
for x in x1..=x2 {
for y in y1..=y2 {
if !s.contains(&(x * n + y)) {
return 0;
}
}
}
1
};
let mut ans = 0;
for a in artifacts {
ans += check(&a);
}
ans
}
}
```

Expand Down
Loading