[프로그래머스] 바탕화면 정리

최유나·2024년 10월 14일
0

프로그래머스

목록 보기
51/53
post-thumbnail

✨ 바탕화면 정리

나의 풀이

function solution(wallpaper) {
    // 위치 변수
    let lux = wallpaper.length; // 맨위
    let luy = wallpaper[0].length; // 맨왼쪽
    let rdx = 0; // 맨아래
    let rdy = 0; // 맨오른
    
    // 최소 범위 파일 위치
    for(let i = 0; i < wallpaper.length; i++){
        for(let j = 0; j < wallpaper[i].length; j++){
            if(wallpaper[i][j] === '#'){
                lux = Math.min(lux, i);
                luy = Math.min(luy, j);
                rdx = Math.max(rdx, i);
                rdy = Math.max(rdy, j);
            }
        }
    }
    // 맨 아래 파일 포함 끝점
    return [lux, luy, rdx + 1, rdy + 1];
}

다른사람의 풀이

function solution(wallpaper) {
    let left = [];
    let top = [];
    let right = []
    let bottom = [];
    wallpaper.forEach((v,i) => {
        [...v].forEach((val,ind) => {
            if(val === "#") {
                left.push(i)
                top.push(ind)
                right.push(i + 1)
                bottom.push(ind + 1)
            }
        })
    })
    return [Math.min(...left), Math.min(...top), Math.max(...right), Math.max(...bottom)]
}

0개의 댓글

관련 채용 정보