[LeetCode] 1030. Matrix Cells in Distance Order

Chobby·2025년 6월 30일
1

LeetCode

목록 보기
450/479

😎풀이

  1. 모든 메트릭스의 좌표 수집
  2. 멘하튼 거리를 기준으로 좌표 정렬
  3. 정렬된 좌표 반환
function allCellsDistOrder(rows: number, cols: number, rCenter: number, cCenter: number): number[][] {
    const pos = []
    for(let row = 0; row < rows; row++) {
        for(let col = 0; col < cols; col++) {
            pos.push([row, col])
        }
    }
    const sortedPos = pos.toSorted(([ar, ac], [br, bc]) => {
        const aDist = Math.abs(ar - rCenter) + Math.abs(ac - cCenter)
        const bDist = Math.abs(br - rCenter) + Math.abs(bc - cCenter)
        return aDist - bDist
    })
    return sortedPos
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글