diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md index f238564b5fec8..02bdee8d3cbe1 100644 --- a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md @@ -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)$。 diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md index c54cd518a8b59..65a8559481167 100644 --- a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md @@ -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)$. diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md index 65a0e9d7131cc..c7305f69796ea 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md @@ -66,6 +66,12 @@ +**方法一:哈希表** + +我们可以用哈希表 $s$ 记录所有挖掘的单元格,然后遍历所有工件,判断工件的所有部分是否都在哈希表中,若是则可以提取该工件,答案加一。 + +时间复杂度 $O(m + k)$,空间复杂度 $O(k)$,其中 $m$ 是工件的数量,而 $k$ 是挖掘的单元格的数量。 + ### **Python3** @@ -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** @@ -95,55 +99,32 @@ class Solution: ```java class Solution { + private Set s = new HashSet<>(); + private int n; + public int digArtifacts(int n, int[][] artifacts, int[][] dig) { - Set 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 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; } ``` @@ -154,22 +135,25 @@ class Solution { public: int digArtifacts(int n, vector>& artifacts, vector>& dig) { unordered_set 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& a, unordered_set& 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& 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; } }; ``` @@ -177,29 +161,87 @@ public: ### **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 = 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>, dig: Vec>) -> i32 { + let mut s: HashSet = 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 + } } ``` diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README_EN.md b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README_EN.md index 346b76d4c1a89..462b61743b61e 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README_EN.md +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README_EN.md @@ -62,6 +62,12 @@ Thus, we return 1. ## Solutions +**Solution 1: Hash Table** + +We can use a hash table $s$ to record all the excavated cells, then traverse all the workpieces, and check whether all parts of the workpiece are in the hash table. If so, we can extract the workpiece, and the answer is increased by one. + +The time complexity is $O(m + k)$, and the space complexity is $O(k)$. Here, $m$ is the number of workpieces, and $k$ is the number of excavated cells. + ### **Python3** @@ -71,71 +77,46 @@ 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** ```java class Solution { + private Set s = new HashSet<>(); + private int n; + public int digArtifacts(int n, int[][] artifacts, int[][] dig) { - Set 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 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; } ``` @@ -146,22 +127,25 @@ class Solution { public: int digArtifacts(int n, vector>& artifacts, vector>& dig) { unordered_set 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& a, unordered_set& 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& 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; } }; ``` @@ -169,29 +153,87 @@ public: ### **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 = 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>, dig: Vec>) -> i32 { + let mut s: HashSet = 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 + } } ``` diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp index ec2da4b9a83d0..f3898893ed415 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp @@ -1,22 +1,25 @@ -class Solution { -public: - int digArtifacts(int n, vector>& artifacts, vector>& dig) { - unordered_set 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& a, unordered_set& 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; - } - } - } - return true; - } +class Solution { +public: + int digArtifacts(int n, vector>& artifacts, vector>& dig) { + unordered_set s; + for (auto& p : dig) { + s.insert(p[0] * n + p[1]); + } + auto check = [&](vector& 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 ans; + } }; \ No newline at end of file diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.go b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.go index b795d0e3fe24e..53d08a4047fda 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.go +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.go @@ -1,24 +1,21 @@ -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 } \ No newline at end of file diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.java b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.java index 4f7476d330941..e6374a6fc62d8 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.java +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.java @@ -1,27 +1,28 @@ -class Solution { - public int digArtifacts(int n, int[][] artifacts, int[][] dig) { - Set s = new HashSet<>(); - for (int[] d : dig) { - s.add(d[0] * n + d[1]); - } - int ans = 0; - for (int[] a : artifacts) { - if (check(a, s, n)) { - ++ans; - } - } - return ans; - } - - private boolean check(int[] a, Set 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; - } +class Solution { + private Set s = new HashSet<>(); + private int n; + + public int digArtifacts(int n, int[][] artifacts, int[][] dig) { + this.n = n; + for (var p : dig) { + s.add(p[0] * n + p[1]); + } + int ans = 0; + for (var a : artifacts) { + ans += check(a); + } + return ans; + } + + 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; + } + } + } + return 1; + } } \ No newline at end of file diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.py b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.py index d187198cc6591..66e068e36e4a5 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.py +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.py @@ -1,14 +1,12 @@ -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 - - s = {(i, j) for i, j in dig} - return sum(check(v) for v in artifacts) +class Solution: + def digArtifacts( + self, n: int, artifacts: List[List[int]], dig: List[List[int]] + ) -> int: + 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(a) for a in artifacts) diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.rs b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.rs new file mode 100644 index 0000000000000..68bb7d917aec3 --- /dev/null +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.rs @@ -0,0 +1,29 @@ +use std::collections::HashSet; + +impl Solution { + pub fn dig_artifacts(n: i32, artifacts: Vec>, dig: Vec>) -> i32 { + let mut s: HashSet = 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 + } +} diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.ts b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.ts index 079c21730b892..ca29a59ddc682 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.ts +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.ts @@ -1,19 +1,22 @@ 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; + const s: Set = new Set(); + for (const [x, y] of dig) { + s.add(x * n + y); } 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; + 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; } } } - flag && ans++; + return 1; + }; + for (const a of artifacts) { + ans += check(a); } return ans; }