Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Find all K nearest neighbor subscripts in the array

You are given an array of integers with indices starting at 0 nums and two integers key and k. K neighbor is one of the nums subscript subscript I, and there are at least a subscript j make | I – j | < = K and nums = = [j] key.

Returns all K nearest neighbor subscripts in ascending order as a list.

Example 1:

Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1 output: [1,2,3,4,5,6] description: therefore, nums[2] == key and nums[5] == key.

  • The subscript 0, 2 | | 0 – > k and | | 0 to 5 > k, so there is no make | 0 – j j | < = k and nums = = key [j]. So 0 is not a K nearest neighbor index.
  • The subscript 1, 1-2 | | < = k and nums [2] = = key, so 1 is a k neighbor subscript.
  • The subscript 2, 2-2 | | < = k and nums [2] = = key, so 2 is a k neighbor subscript.
  • The subscript 3, 2 | | 3 – < = k and nums [2] = = key, so 3 is a k neighbor subscript.
  • The subscript 4, 4-5 | | < = k and nums [5] = = key, so is a 4 k neighbor subscript.
  • The subscript 5, 5-5 | | < = k and nums [5] = = key, so 5 is a k neighbor subscript.
  • The subscript 6, 6-5 | | < = k and nums [5] = = key, so 6 is a k neighbor subscript.

Therefore, return [1,2,3,4,5,6] in ascending order.

Example 2:

Input: nums =,2,2,2,2 [2], the key = 2 and k = 2 output:,1,2,3,4 [0] interpretation: All the subscript nums I, there is always a subscript j make | I – j | < = k and nums = = [j] key, so each subscript is a k neighbor subscript. Therefore, return [0,1,2,3,4].

Tip:

1 <= nums.length <= 1000 1 <= nums[I] <= 1000 Key is an integer in the array nums 1 <= k <= nums.length

Subject analysis

Let’s find all K nearest subscripts in the array. K is a range [0, K]. In this case, we need to iterate through the array to find elements with the same key. Find the subscript difference between this element and this element and then [0,k].

Distribution of implementation

First find the element in the array that is the same as key

for (let i = 0; i < nums.length; i ++) {
    if(nums[i] === key){
    
    }
}
Copy the code

Determine the range based on the index of the element in the array that is the same as the key.

for (let i = 0; i < nums.length; i ++) {
    if(nums[i] === key){
        let l = Math.max(0, i - k)
        let r = Math.min(i + k, nums.length - 1)}}Copy the code

Finally, we store the subscript in the set variable, which we can convert to an array using the FROM method.

let set = new Set(a)for (let i = 0; i < nums.length; i ++) {
    if(nums[i] === key){
        let l = Math.max(0, i - k)
        let r = Math.min(i + k, nums.length - 1)
        for (let j = l; j <= r; j ++) {
            if(! set.has(j)) { set.add(j) } } } }return Array.from(set)
Copy the code

Code implementation

/ * * *@param {number[]} nums
 * @param {number} key
 * @param {number} k
 * @return {number[]}* /
var findKDistantIndices = function(nums, key, k) {
    const set = new Set(a)for (let i = 0; i < nums.length; i ++) {
        const num = nums[i]
        if (num === key) {
            let l = Math.max(0, i - k)
            let r = Math.min(i + k, nums.length - 1)
            for (let j = l; j <= r; j ++) {
                if(! set.has(j)) { set.add(j) } } } }return Array.from(set)
};
Copy the code