Skip to content

Commit 44ac3fd

Browse files
committed
2200. Find All K-Distant Indices in an Array: AC
1 parent dcf3d4c commit 44ac3fd

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,3 +1663,4 @@ mod s2194_cells_in_a_range_on_an_excel_sheet;
16631663
mod s2195_append_k_integers_with_minimal_sum;
16641664
mod s2196_create_binary_tree_from_descriptions;
16651665
mod s2197_replace_non_coprime_numbers_in_array;
1666+
mod s2200_find_all_k_distant_indices_in_an_array;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* [2200] Find All K-Distant Indices in an Array
3+
*
4+
* You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key.
5+
* Return a list of all k-distant indices sorted in increasing order.
6+
*
7+
* Example 1:
8+
*
9+
* Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1
10+
* Output: [1,2,3,4,5,6]
11+
* Explanation: Here, nums[2] == key and nums[5] == key.
12+
* - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index.
13+
* - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
14+
* - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
15+
* - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
16+
* - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
17+
* - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
18+
* - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
19+
* Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
20+
*
21+
* Example 2:
22+
*
23+
* Input: nums = [2,2,2,2,2], key = 2, k = 2
24+
* Output: [0,1,2,3,4]
25+
* Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
26+
* Hence, we return [0,1,2,3,4].
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* 1 <= nums.length <= 1000
32+
* 1 <= nums[i] <= 1000
33+
* key is an integer from the array nums.
34+
* 1 <= k <= nums.length
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
40+
// discuss: https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
46+
(0..nums.len())
47+
.filter_map(|i| {
48+
nums[i.saturating_sub(k as usize)..=(i + k as usize).min(nums.len() - 1)]
49+
.contains(&key)
50+
.then_some(i as i32)
51+
})
52+
.collect()
53+
}
54+
}
55+
56+
// submission codes end
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn test_2200_example_1() {
64+
let nums = vec![3, 4, 9, 1, 3, 9, 5];
65+
let key = 9;
66+
let k = 1;
67+
68+
let result = vec![1, 2, 3, 4, 5, 6];
69+
70+
assert_eq!(Solution::find_k_distant_indices(nums, key, k), result);
71+
}
72+
73+
#[test]
74+
fn test_2200_example_2() {
75+
let nums = vec![2, 2, 2, 2, 2];
76+
let key = 2;
77+
let k = 2;
78+
79+
let result = vec![0, 1, 2, 3, 4];
80+
81+
assert_eq!(Solution::find_k_distant_indices(nums, key, k), result);
82+
}
83+
}

0 commit comments

Comments
 (0)