Find All K-Distant Indices in an Array LeetCode solution in Python
1class Solution:2def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:3lis = deque([])4prev_popped = -15for i in range(len(nums)):6if nums[i] == key:7lis.append(i)8ans = []9for i in range(len(nums)):10if len(lis) > 0 and lis[0] < i:11prev_popped = lis.popleft()12if prev_popped != -1 and (i - prev_popped) <= k:13ans.append(i)14elif len(lis) > 0 and (lis[0] - i) <= k:15ans.append(i)16return ans