[LeetCode] 2200. Find All K-Distant Indices in an Array

Chobby·4일 전
1

LeetCode

목록 보기
686/710

😎풀이

  1. nums 순회
    1-1. 만약 nums[i]key와 다를 경우 생략
    1-2. nums 순회
    1-3. |i - j|k를 초과하는 경우 생략
    1-4. 현재 인덱스를 k distant로 취급
  2. 중복이 제거된 k distant 목록 반환
function findKDistantIndices(nums: number[], key: number, k: number): number[] {
    const kDistants = new Set<number>()
    for(let i = 0; i < nums.length; i++) {
        if(nums[i] !== key) continue
        for(let j = 0; j < nums.length; j++) {
            if(Math.abs(i - j) > k) continue
            kDistants.add(j)
        }
    }
    return [...kDistants]
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글