Skip to content

가장 큰 정사각형 찾기#156

Merged
NamJwong merged 2 commits intomainfrom
algorithm/#156
May 6, 2023
Merged

가장 큰 정사각형 찾기#156
NamJwong merged 2 commits intomainfrom
algorithm/#156

Conversation

@NamJwong
Copy link
Copy Markdown
Member

@NamJwong NamJwong commented May 6, 2023

문제

https://school.programmers.co.kr/learn/courses/30/lessons/12905

풀이 후기

BFS 비슷하게 풀었다가 케이스도 몇 개 틀리고, 아예 시간 초과가 나서 도대체 어떻게 풀지? 하고 풀이를 찾아봤더니 DP 문제였다.
이런 구상은 어떻게 하는 거지 .. ㅠ 문제를 많이 풀어야겠다.

생각해보니 BFS는 최대 N^2지만, 내가 작성한 BFS 비스무리한 코드는 N^2가 넘어간다..! N이 1,000이니 시간 초과는 당연했다ㅠ

다른 사람의 풀이와 비교

좋아요 2등 풀이

function solution(board)
{
    let max = 0;
    let x = board.length;
    let y = board[0].length;

    if(x<=1 || y<=1) return 1;

    for(let i=1; i<x; i++){
        for(let j=1; j<y; j++){
            if(board[i][j] >= 1){
                let min = Math.min(board[i-1][j], board[i-1][j-1], board[i][j-1]);

                board[i][j] = min+1;
                max = Math.max(max, min+1);
            }
        }
    }

    return max*max;
}

이렇게 for문을 통해 인덱스 1부터 탐색하게 해서 내 코드의 board[x - 1] && board[x - 1][y - 1] 조건 검사를 안 하게 해주는 것도 방법이다.

@NamJwong NamJwong self-assigned this May 6, 2023
@NamJwong NamJwong merged commit 428a17d into main May 6, 2023
@NamJwong NamJwong deleted the algorithm/#156 branch May 6, 2023 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant