😎풀이

  1. 컬럼 최댓값 조회 헬퍼 함수 정의
  2. 모든 컬럼의 최댓값 사전 저장
  3. matrix 복제
  4. 복제된 2차원 배열 순회
    4-1. 현재 요소가 -1인 경우 해당 컬럼의 최댓값으로 갱신
  5. 갱신된 복제 배열 반환
function modifiedMatrix(matrix: number[][]): number[][] {
    const colMaxies = []
    for(let col = 0; col < matrix[0].length; col++) {
        const curColMax = findColMax(matrix, col)
        colMaxies.push(curColMax)
    }
    const cloned = structuredClone(matrix)
    for(let row = 0; row < matrix.length; row++) {
        for(let col = 0; col < matrix[0].length; col++) {
            if(cloned[row][col] !== -1) continue
            cloned[row][col] = colMaxies[col]
        }
    }
    return cloned
};

function findColMax(matrix: number[][], col: number) {
    let max = -1
    for(let row = 0; row < matrix.length; row++) {
       max = Math.max(max, matrix[row][col])
    }
    return max
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글