Find All K-Distant Indices in an Array LeetCode solution in Python

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0