[LeetCode] 2903. Find Indices With Index and Value Difference I

Chobby·어제

LeetCode

목록 보기
819/826

😎풀이

  1. nums 2중 순회
    1-1. 동일 인덱스도 가능하며, 두 인덱스의 차이가, indexDifference 이상인지 판별
    1-2. 두 인덱스 값의 차이가, valueDifference 이상인지 판별
    1-3. 두 조건을 모두 만족한 경우 두 인덱스를 배열에 담아 반환
  2. 만족되는 경우가 없을 경우, [-1, -1] 반환
function findIndices(nums: number[], indexDifference: number, valueDifference: number): number[] {
    for(let i = 0; i < nums.length; i++) {
        for(let j = i; j < nums.length; j++) {
            const idxGap = j - i
            if(idxGap < indexDifference) continue
            const valGap = Math.abs(nums[i] - nums[j])
            if(valGap < valueDifference) continue
            return [i, j]
        }
    }
    return [-1, -1]
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글