LeetCode 2200. Find All K-Distant Indices in an Array Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2200. Find All K-Distant Indices in an Array

Description

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.

Return a list of all k-distant indices sorted in increasing order.

 

Example 1:

Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1
Output: [1,2,3,4,5,6]
Explanation: Here, nums[2] == key and nums[5] == key.
- 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.
- For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
- For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
- For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
- For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
- For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
- For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
Thus, we return [1,2,3,4,5,6] which is sorted in increasing order. 

Example 2:

Input: nums = [2,2,2,2,2], key = 2, k = 2
Output: [0,1,2,3,4]
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. 
Hence, we return [0,1,2,3,4].

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • key is an integer from the array nums.
  • 1 <= k <= nums.length

Solutions

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| ≤ 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(n2), where n is the length of the array nums. The space complexity is O(1).

PythonJavaC++GoTypeScriptRust
class Solution: def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]: ans = [] n = len(nums) for i in range(n): if any(abs(i - j) <= k and nums[j] == key for j in range(n)): ans.append(i) return ans(code-box)

Solution 2: Preprocessing + Binary Search

We can preprocess to get the indices of all elements equal to key, recorded in the array idx. All index elements in the array idx are sorted in ascending order.

Next, we enumerate the index i. For each index i, we can use binary search to find elements in the range [i - k, i + k] in the array idx. If there are elements, then i is a K-nearest neighbor index. We add i to the answer array.

The time complexity is O(n × log n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptRust
class Solution: def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]: idx = [i for i, x in enumerate(nums) if x == key] ans = [] for i in range(len(nums)): l = bisect_left(idx, i - k) r = bisect_right(idx, i + k) - 1 if l <= r: ans.append(i) return ans(code-box)

Solution 3: Two Pointers

We enumerate the index i, and use a pointer j to point to the smallest index that satisfies j ≥ i - k and nums[j] = key. If j exists and j ≤ i + k, then i is a K-nearest neighbor index. We add i to the answer array.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

PythonJavaC++GoTypeScriptRust
class Solution: def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]: ans = [] j, n = 0, len(nums) for i in range(n): while j < i - k or (j < n and nums[j] != key): j += 1 if j < n and j <= (i + k): ans.append(i) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !