-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2200.java
More file actions
33 lines (26 loc) · 1.1 KB
/
Copy pathLC2200.java
File metadata and controls
33 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
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.
*/
import java.util.List;
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> kDistantIndices = new ArrayList<>();
//Using toBeAdded to prevent dulpicates in the final list
//Sets can be used but then they have to be transformed to a List
//and also will have to be sorted increasing runtime
boolean[] toBeAdded = new boolean[nums.length];
for (int i=0; i<nums.length; i++) {
if (nums[i] == key) {
for (int j = Math.max(0, (i-k)); j < Math.min((nums.length), (i+k+1)); j++) {
toBeAdded[j] = true;
}
}
}
for (int i=0; i<nums.length; i++) {
if (toBeAdded[i]) kDistantIndices.add(i);
}
return kDistantIndices;
}
}
//Solved