Skip to content

Commit c32b063

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (73.83%), Memory - 17.9 MB (68.60%)
1 parent 7c52cc0 commit c32b063

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>key</code> and <code>k</code>. A <strong>k-distant index</strong> is an index <code>i</code> of <code>nums</code> for which there exists at least one index <code>j</code> such that <code>|i - j| &lt;= k</code> and <code>nums[j] == key</code>.</p>
2+
3+
<p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [3,4,9,1,3,9,5], key = 9, k = 1
10+
<strong>Output:</strong> [1,2,3,4,5,6]
11+
<strong>Explanation:</strong> Here, <code>nums[2] == key</code> and <code>nums[5] == key.
12+
- For index 0, |0 - 2| &gt; k and |0 - 5| &gt; k, so there is no j</code> where <code>|0 - j| &lt;= k</code> and <code>nums[j] == key. Thus, 0 is not a k-distant index.
13+
- For index 1, |1 - 2| &lt;= k and nums[2] == key, so 1 is a k-distant index.
14+
- For index 2, |2 - 2| &lt;= k and nums[2] == key, so 2 is a k-distant index.
15+
- For index 3, |3 - 2| &lt;= k and nums[2] == key, so 3 is a k-distant index.
16+
- For index 4, |4 - 5| &lt;= k and nums[5] == key, so 4 is a k-distant index.
17+
- For index 5, |5 - 5| &lt;= k and nums[5] == key, so 5 is a k-distant index.
18+
- For index 6, |6 - 5| &lt;= k and nums[5] == key, so 6 is a k-distant index.
19+
</code>Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [2,2,2,2,2], key = 2, k = 2
26+
<strong>Output:</strong> [0,1,2,3,4]
27+
<strong>Explanation:</strong> For all indices i in nums, there exists some index j such that |i - j| &lt;= k and nums[j] == key, so every index is a k-distant index.
28+
Hence, we return [0,1,2,3,4].
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
36+
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
37+
<li><code>key</code> is an integer from the array <code>nums</code>.</li>
38+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
39+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 2: One-time Traversal
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
8+
res = []
9+
r = 0
10+
n = len(nums)
11+
12+
for j in range(n):
13+
if nums[j] == key:
14+
l = max(r, j - k)
15+
r = min(n - 1, j + k) + 1
16+
for i in range(l, r):
17+
res.append(i)
18+
19+
return res
20+

0 commit comments

Comments
 (0)