66
66
67
67
<!-- 这里可写通用的实现逻辑 -->
68
68
69
+ ** 方法一:哈希表**
70
+
71
+ 我们可以用哈希表 $s$ 记录所有挖掘的单元格,然后遍历所有工件,判断工件的所有部分是否都在哈希表中,若是则可以提取该工件,答案加一。
72
+
73
+ 时间复杂度 $O(m + k)$,空间复杂度 $O(k)$,其中 $m$ 是工件的数量,而 $k$ 是挖掘的单元格的数量。
74
+
69
75
<!-- tabs:start -->
70
76
71
77
### ** Python3**
@@ -77,16 +83,14 @@ class Solution:
77
83
def digArtifacts (
78
84
self , n : int , artifacts : List[List[int ]], dig : List[List[int ]]
79
85
) -> 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
+ )
87
91
88
92
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)
90
94
```
91
95
92
96
### ** Java**
@@ -95,55 +99,32 @@ class Solution:
95
99
96
100
``` java
97
101
class Solution {
102
+ private Set<Integer > s = new HashSet<> ();
103
+ private int n;
104
+
98
105
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 ]);
102
109
}
103
110
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);
108
113
}
109
114
return ans;
110
115
}
111
116
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 ;
141
123
}
142
124
}
143
125
}
144
- flag && ans ++ ;
126
+ return 1 ;
145
127
}
146
- return ans ;
147
128
}
148
129
```
149
130
@@ -154,52 +135,113 @@ class Solution {
154
135
public:
155
136
int digArtifacts(int n, vector<vector<int >>& artifacts, vector<vector<int >>& dig) {
156
137
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
+ }
169
148
}
170
149
}
150
+ return 1;
151
+ };
152
+ int ans = 0;
153
+ for (auto& a : artifacts) {
154
+ ans += check(a);
171
155
}
172
- return true ;
156
+ return ans ;
173
157
}
174
158
};
175
159
```
176
160
177
161
### **Go**
178
162
179
163
```go
180
- func digArtifacts (n int , artifacts [][]int , dig [][]int ) int {
164
+ func digArtifacts(n int, artifacts [][]int, dig [][]int) (ans int) {
181
165
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
184
168
}
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
191
175
}
192
176
}
193
177
}
194
- return true
178
+ return 1
195
179
}
196
- ans := 0
197
180
for _, a := range artifacts {
198
- if check (a) {
199
- ans++
200
- }
181
+ ans += check(a)
201
182
}
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
+ }
203
245
}
204
246
```
205
247
0 commit comments