Leetcode # 2200. Find All K-Distant Indices in an Array
- 2025.06.24
- ★ Easy LeetCode Two Pointers
Problem
https://leetcode.com/problems/find-all-k-distant-indices-in-an-array
Solution
Time Complexity: O(len(nums))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)
class Solution:
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
ans = []
for j, num in enumerate(nums):
if num == key:
ans += [i for i in range(
max(0 if len(ans) == 0 else (ans[-1] + 1), j - k),
min(j + k + 1, len(nums)))]
return ans
Last Updated on 2025/06/24 by A1go