Problem Solving/Programmers

[Level 2] 가장 큰 정사각형 찾기

kmkunk 2022. 4. 7. 18:46
class Solution {
    public int solution(int [][]board) {
        int answer = 0;
        int[][] dp = new int[board.length][board[0].length];
        
        for(int i=0; i<dp.length; i++) {
            for(int j=0; j<dp[0].length; j++) { dp[i][j] = board[i][j]; }
        }
        
        for(int i=1; i<dp.length; i++) {
            for(int j=1; j<dp[0].length; j++) {
                if(dp[i][j]==0) { continue; }
                if(dp[i-1][j-1]>0 && dp[i][j-1]>0 && dp[i-1][j]>0) {
                    dp[i][j] = Math.min(Math.min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1])+1;
                }
            }
        }
        
        for(int i=0; i<dp.length; i++ ) {
            for(int j=0; j<dp[0].length; j++) { answer = Math.max(answer, dp[i][j]); }
        }
        
        return answer*answer;
    }
}