Skip to content

Commit a28b9fd

Browse files
Merge branch 'main' into main
2 parents ce720bc + 18a93fa commit a28b9fd

File tree

8 files changed

+519
-2
lines changed

8 files changed

+519
-2
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
178+
let n = nums.len();
179+
let mut ans = Vec::new();
180+
for i in 0..n {
181+
for j in 0..n {
182+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
183+
ans.push(i as i32);
184+
break;
185+
}
186+
}
187+
}
188+
ans
189+
}
190+
}
191+
```
192+
173193
<!-- tabs:end -->
174194

175195
<!-- solution:end -->
@@ -309,6 +329,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
309329
}
310330
```
311331

332+
#### Rust
333+
334+
```rust
335+
impl Solution {
336+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
337+
let n = nums.len();
338+
let mut idx = Vec::new();
339+
for i in 0..n {
340+
if nums[i] == key {
341+
idx.push(i as i32);
342+
}
343+
}
344+
345+
let search = |x: i32| -> usize {
346+
let (mut l, mut r) = (0, idx.len());
347+
while l < r {
348+
let mid = (l + r) >> 1;
349+
if idx[mid] >= x {
350+
r = mid;
351+
} else {
352+
l = mid + 1;
353+
}
354+
}
355+
l
356+
};
357+
358+
let mut ans = Vec::new();
359+
for i in 0..n {
360+
let l = search(i as i32 - k);
361+
let r = search(i as i32 + k + 1) as i32 - 1;
362+
if l as i32 <= r {
363+
ans.push(i as i32);
364+
}
365+
}
366+
367+
ans
368+
}
369+
}
370+
```
371+
312372
<!-- tabs:end -->
313373

314374
<!-- solution:end -->
@@ -414,6 +474,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
414474
}
415475
```
416476

477+
#### Rust
478+
479+
```rust
480+
impl Solution {
481+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
482+
let n = nums.len();
483+
let mut ans = Vec::new();
484+
let mut j = 0;
485+
for i in 0..n {
486+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
487+
j += 1;
488+
}
489+
if j < n && j <= i + k as usize {
490+
ans.push(i as i32);
491+
}
492+
}
493+
ans
494+
}
495+
}
496+
```
497+
417498
<!-- tabs:end -->
418499

419500
<!-- solution:end -->

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
168168
}
169169
```
170170

171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
176+
let n = nums.len();
177+
let mut ans = Vec::new();
178+
for i in 0..n {
179+
for j in 0..n {
180+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
181+
ans.push(i as i32);
182+
break;
183+
}
184+
}
185+
}
186+
ans
187+
}
188+
}
189+
```
190+
171191
<!-- tabs:end -->
172192

173193
<!-- solution:end -->
@@ -307,6 +327,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
307327
}
308328
```
309329

330+
#### Rust
331+
332+
```rust
333+
impl Solution {
334+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
335+
let n = nums.len();
336+
let mut idx = Vec::new();
337+
for i in 0..n {
338+
if nums[i] == key {
339+
idx.push(i as i32);
340+
}
341+
}
342+
343+
let search = |x: i32| -> usize {
344+
let (mut l, mut r) = (0, idx.len());
345+
while l < r {
346+
let mid = (l + r) >> 1;
347+
if idx[mid] >= x {
348+
r = mid;
349+
} else {
350+
l = mid + 1;
351+
}
352+
}
353+
l
354+
};
355+
356+
let mut ans = Vec::new();
357+
for i in 0..n {
358+
let l = search(i as i32 - k);
359+
let r = search(i as i32 + k + 1) as i32 - 1;
360+
if l as i32 <= r {
361+
ans.push(i as i32);
362+
}
363+
}
364+
365+
ans
366+
}
367+
}
368+
```
369+
310370
<!-- tabs:end -->
311371

312372
<!-- solution:end -->
@@ -412,6 +472,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
412472
}
413473
```
414474

475+
#### Rust
476+
477+
```rust
478+
impl Solution {
479+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
480+
let n = nums.len();
481+
let mut ans = Vec::new();
482+
let mut j = 0;
483+
for i in 0..n {
484+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
485+
j += 1;
486+
}
487+
if j < n && j <= i + k as usize {
488+
ans.push(i as i32);
489+
}
490+
}
491+
ans
492+
}
493+
}
494+
```
495+
415496
<!-- tabs:end -->
416497

417498
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut ans = Vec::new();
5+
for i in 0..n {
6+
for j in 0..n {
7+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
8+
ans.push(i as i32);
9+
break;
10+
}
11+
}
12+
}
13+
ans
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut idx = Vec::new();
5+
for i in 0..n {
6+
if nums[i] == key {
7+
idx.push(i as i32);
8+
}
9+
}
10+
11+
let search = |x: i32| -> usize {
12+
let (mut l, mut r) = (0, idx.len());
13+
while l < r {
14+
let mid = (l + r) >> 1;
15+
if idx[mid] >= x {
16+
r = mid;
17+
} else {
18+
l = mid + 1;
19+
}
20+
}
21+
l
22+
};
23+
24+
let mut ans = Vec::new();
25+
for i in 0..n {
26+
let l = search(i as i32 - k);
27+
let r = search(i as i32 + k + 1) as i32 - 1;
28+
if l as i32 <= r {
29+
ans.push(i as i32);
30+
}
31+
}
32+
33+
ans
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut ans = Vec::new();
5+
let mut j = 0;
6+
for i in 0..n {
7+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
8+
j += 1;
9+
}
10+
if j < n && j <= i + k as usize {
11+
ans.push(i as i32);
12+
}
13+
}
14+
ans
15+
}
16+
}

solution/3500-3599/3590.Kth Smallest Path XOR Sum/README.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,103 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3590.Kt
128128
#### Python3
129129

130130
```python
131-
131+
class BinarySumTrie:
132+
def __init__(self):
133+
self.count = 0
134+
self.children = [None, None]
135+
136+
def add(self, num: int, delta: int, bit=17):
137+
self.count += delta
138+
if bit < 0:
139+
return
140+
b = (num >> bit) & 1
141+
if not self.children[b]:
142+
self.children[b] = BinarySumTrie()
143+
self.children[b].add(num, delta, bit - 1)
144+
145+
def collect(self, prefix=0, bit=17, output=None):
146+
if output is None:
147+
output = []
148+
if self.count == 0:
149+
return output
150+
if bit < 0:
151+
output.append(prefix)
152+
return output
153+
if self.children[0]:
154+
self.children[0].collect(prefix, bit - 1, output)
155+
if self.children[1]:
156+
self.children[1].collect(prefix | (1 << bit), bit - 1, output)
157+
return output
158+
159+
def exists(self, num: int, bit=17):
160+
if self.count == 0:
161+
return False
162+
if bit < 0:
163+
return True
164+
b = (num >> bit) & 1
165+
return self.children[b].exists(num, bit - 1) if self.children[b] else False
166+
167+
def find_kth(self, k: int, bit=17):
168+
if k > self.count:
169+
return -1
170+
if bit < 0:
171+
return 0
172+
left_count = self.children[0].count if self.children[0] else 0
173+
if k <= left_count:
174+
return self.children[0].find_kth(k, bit - 1)
175+
elif self.children[1]:
176+
return (1 << bit) + self.children[1].find_kth(k - left_count, bit - 1)
177+
else:
178+
return -1
179+
180+
181+
class Solution:
182+
def kthSmallest(
183+
self, par: List[int], vals: List[int], queries: List[List[int]]
184+
) -> List[int]:
185+
n = len(par)
186+
tree = [[] for _ in range(n)]
187+
for i in range(1, n):
188+
tree[par[i]].append(i)
189+
190+
path_xor = vals[:]
191+
narvetholi = path_xor
192+
193+
def compute_xor(node, acc):
194+
path_xor[node] ^= acc
195+
for child in tree[node]:
196+
compute_xor(child, path_xor[node])
197+
198+
compute_xor(0, 0)
199+
200+
node_queries = defaultdict(list)
201+
for idx, (u, k) in enumerate(queries):
202+
node_queries[u].append((k, idx))
203+
204+
trie_pool = {}
205+
result = [0] * len(queries)
206+
207+
def dfs(node):
208+
trie_pool[node] = BinarySumTrie()
209+
trie_pool[node].add(path_xor[node], 1)
210+
for child in tree[node]:
211+
dfs(child)
212+
if trie_pool[node].count < trie_pool[child].count:
213+
trie_pool[node], trie_pool[child] = (
214+
trie_pool[child],
215+
trie_pool[node],
216+
)
217+
for val in trie_pool[child].collect():
218+
if not trie_pool[node].exists(val):
219+
trie_pool[node].add(val, 1)
220+
for k, idx in node_queries[node]:
221+
if trie_pool[node].count < k:
222+
result[idx] = -1
223+
else:
224+
result[idx] = trie_pool[node].find_kth(k)
225+
226+
dfs(0)
227+
return result
132228
```
133229

134230
#### Java

0 commit comments

Comments
 (0)