Skip to content

Commit f73ca55

Browse files
authored
feat: add solutions to lc problem: No.2200 (doocs#2091)
No.2200.Find All K-Distant Indices in an Array
1 parent 8eef402 commit f73ca55

File tree

10 files changed

+351
-236
lines changed

10 files changed

+351
-236
lines changed

solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
**方法一:枚举**
5656

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

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

solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Hence, we return [0,1,2,3,4].
4848

4949
**Solution 1: Enumeration**
5050

51-
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$.
51+
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$.
5252

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

solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md

+116-74
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666

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

69+
**方法一:哈希表**
70+
71+
我们可以用哈希表 $s$ 记录所有挖掘的单元格,然后遍历所有工件,判断工件的所有部分是否都在哈希表中,若是则可以提取该工件,答案加一。
72+
73+
时间复杂度 $O(m + k)$,空间复杂度 $O(k)$,其中 $m$ 是工件的数量,而 $k$ 是挖掘的单元格的数量。
74+
6975
<!-- tabs:start -->
7076

7177
### **Python3**
@@ -77,16 +83,14 @@ class Solution:
7783
def digArtifacts(
7884
self, n: int, artifacts: List[List[int]], dig: List[List[int]]
7985
) -> int:
80-
def check(artifact):
81-
r1, c1, r2, c2 = artifact
82-
for x in range(r1, r2 + 1):
83-
for y in range(c1, c2 + 1):
84-
if (x, y) not in s:
85-
return False
86-
return True
86+
def check(a: List[int]) -> bool:
87+
x1, y1, x2, y2 = a
88+
return all(
89+
(x, y) in s for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)
90+
)
8791

8892
s = {(i, j) for i, j in dig}
89-
return sum(check(v) for v in artifacts)
93+
return sum(check(a) for a in artifacts)
9094
```
9195

9296
### **Java**
@@ -95,55 +99,32 @@ class Solution:
9599

96100
```java
97101
class Solution {
102+
private Set<Integer> s = new HashSet<>();
103+
private int n;
104+
98105
public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
99-
Set<Integer> s = new HashSet<>();
100-
for (int[] d : dig) {
101-
s.add(d[0] * n + d[1]);
106+
this.n = n;
107+
for (var p : dig) {
108+
s.add(p[0] * n + p[1]);
102109
}
103110
int ans = 0;
104-
for (int[] a : artifacts) {
105-
if (check(a, s, n)) {
106-
++ans;
107-
}
111+
for (var a : artifacts) {
112+
ans += check(a);
108113
}
109114
return ans;
110115
}
111116

112-
private boolean check(int[] a, Set<Integer> s, int n) {
113-
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
114-
for (int i = r1; i <= r2; ++i) {
115-
for (int j = c1; j <= c2; ++j) {
116-
if (!s.contains(i * n + j)) {
117-
return false;
118-
}
119-
}
120-
}
121-
return true;
122-
}
123-
}
124-
```
125-
126-
### **TypeScript**
127-
128-
```ts
129-
function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {
130-
let visited = Array.from({ length: n }, v => new Array(n).fill(false));
131-
for (let [i, j] of dig) {
132-
visited[i][j] = true;
133-
}
134-
let ans = 0;
135-
for (let [a, b, c, d] of artifacts) {
136-
let flag = true;
137-
for (let i = a; i <= c && flag; i++) {
138-
for (let j = b; j <= d && flag; j++) {
139-
if (!visited[i][j]) {
140-
flag = false;
117+
private int check(int[] a) {
118+
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
119+
for (int x = x1; x <= x2; ++x) {
120+
for (int y = y1; y <= y2; ++y) {
121+
if (!s.contains(x * n + y)) {
122+
return 0;
141123
}
142124
}
143125
}
144-
flag && ans++;
126+
return 1;
145127
}
146-
return ans;
147128
}
148129
```
149130

@@ -154,52 +135,113 @@ class Solution {
154135
public:
155136
int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {
156137
unordered_set<int> s;
157-
for (auto& d : dig) s.insert(d[0] * n + d[1]);
158-
int ans = 0;
159-
for (auto& a : artifacts) ans += check(a, s, n);
160-
return ans;
161-
}
162-
163-
bool check(vector<int>& a, unordered_set<int>& s, int n) {
164-
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
165-
for (int i = r1; i <= r2; ++i) {
166-
for (int j = c1; j <= c2; ++j) {
167-
if (!s.count(i * n + j)) {
168-
return false;
138+
for (auto& p : dig) {
139+
s.insert(p[0] * n + p[1]);
140+
}
141+
auto check = [&](vector<int>& a) {
142+
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
143+
for (int x = x1; x <= x2; ++x) {
144+
for (int y = y1; y <= y2; ++y) {
145+
if (!s.count(x * n + y)) {
146+
return 0;
147+
}
169148
}
170149
}
150+
return 1;
151+
};
152+
int ans = 0;
153+
for (auto& a : artifacts) {
154+
ans += check(a);
171155
}
172-
return true;
156+
return ans;
173157
}
174158
};
175159
```
176160
177161
### **Go**
178162
179163
```go
180-
func digArtifacts(n int, artifacts [][]int, dig [][]int) int {
164+
func digArtifacts(n int, artifacts [][]int, dig [][]int) (ans int) {
181165
s := map[int]bool{}
182-
for _, d := range dig {
183-
s[d[0]*n+d[1]] = true
166+
for _, p := range dig {
167+
s[p[0]*n+p[1]] = true
184168
}
185-
check := func(a []int) bool {
186-
r1, c1, r2, c2 := a[0], a[1], a[2], a[3]
187-
for i := r1; i <= r2; i++ {
188-
for j := c1; j <= c2; j++ {
189-
if !s[i*n+j] {
190-
return false
169+
check := func(a []int) int {
170+
x1, y1, x2, y2 := a[0], a[1], a[2], a[3]
171+
for x := x1; x <= x2; x++ {
172+
for y := y1; y <= y2; y++ {
173+
if !s[x*n+y] {
174+
return 0
191175
}
192176
}
193177
}
194-
return true
178+
return 1
195179
}
196-
ans := 0
197180
for _, a := range artifacts {
198-
if check(a) {
199-
ans++
200-
}
181+
ans += check(a)
201182
}
202-
return ans
183+
return
184+
}
185+
```
186+
187+
### **TypeScript**
188+
189+
```ts
190+
function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {
191+
const s: Set<number> = new Set();
192+
for (const [x, y] of dig) {
193+
s.add(x * n + y);
194+
}
195+
let ans = 0;
196+
const check = (a: number[]): number => {
197+
const [x1, y1, x2, y2] = a;
198+
for (let x = x1; x <= x2; ++x) {
199+
for (let y = y1; y <= y2; ++y) {
200+
if (!s.has(x * n + y)) {
201+
return 0;
202+
}
203+
}
204+
}
205+
return 1;
206+
};
207+
for (const a of artifacts) {
208+
ans += check(a);
209+
}
210+
return ans;
211+
}
212+
```
213+
214+
### **Rust**
215+
216+
```rust
217+
use std::collections::HashSet;
218+
219+
impl Solution {
220+
pub fn dig_artifacts(n: i32, artifacts: Vec<Vec<i32>>, dig: Vec<Vec<i32>>) -> i32 {
221+
let mut s: HashSet<i32> = HashSet::new();
222+
for p in dig {
223+
s.insert(p[0] * n + p[1]);
224+
}
225+
let check = |a: &[i32]| -> i32 {
226+
let x1 = a[0];
227+
let y1 = a[1];
228+
let x2 = a[2];
229+
let y2 = a[3];
230+
for x in x1..=x2 {
231+
for y in y1..=y2 {
232+
if !s.contains(&(x * n + y)) {
233+
return 0;
234+
}
235+
}
236+
}
237+
1
238+
};
239+
let mut ans = 0;
240+
for a in artifacts {
241+
ans += check(&a);
242+
}
243+
ans
244+
}
203245
}
204246
```
205247

0 commit comments

Comments
 (0)