Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.07.06 - [PG] 가장 큰 정사각형 찾기 #113

Closed
suhyunsim opened this issue Jul 7, 2021 · 0 comments
Closed

21.07.06 - [PG] 가장 큰 정사각형 찾기 #113

suhyunsim opened this issue Jul 7, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 lv.2 프로그래머스 - level 2 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • DP로 풀이
  • 2x2 사각형의 우측하단 꼭짓점을 기준으로 정사각형이 되는지 체크한다.
  • 현재 값이 1인경우 좌←, 상↑, 좌상↖ 체크
  • ←, ↑, ↖ 값이 모두 1인경우 정사각형을 만들 수 있음
  • newBoard[i][j] = 정사각형 한 변의 길이

어려운 점, 실수

  • 단순 반복문으로 길이를 찾으려고 했음 -> 시간 초과

풀이

package main.java.com.poogle.PG.Q12905;

public class Solution {
    public static void main(String[] args) {
        System.out.println(solution(new int[][]{{0, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {0, 0, 1, 0}}));
    }

    private static int solution(int[][] board) {
        int[][] newBoard = new int[board.length + 1][board[0].length + 1];
        for (int i = 0; i < board.length; i++) {
            System.arraycopy(board[i], 0, newBoard[i + 1], 1, board[i].length);
        }
        int max = 0;

        for (int i = 1; i < newBoard.length; i++) {
            for (int j = 1; j < newBoard[i].length; j++) {
                /* 2x2 사각형의 우측하단 꼭짓점을 기준으로 정사각형이 되는지 체크한다.
                 * 현재 값이 1인경우 좌←, 상↑, 좌상↖ 체크
                 * ←, ↑, ↖ 값이 모두 1인경우 정사각형을 만들 수 있음
                 */
                //newBoard에 들어가는 값(정사각형 한 변의 길이)
                if (newBoard[i][j] == 1) {
                    newBoard[i][j] = Math.min(Math.min(newBoard[i - 1][j], newBoard[i][j - 1]), newBoard[i - 1][j - 1]) + 1;
                    max = Math.max(max, newBoard[i][j]);
                }
            }
        }
        return max * max;
    }
}
@suhyunsim suhyunsim added 실패 시도했지만 맞지 못한 문제 lv.2 프로그래머스 - level 2 DP 다이나믹 프로그래밍 labels Jul 7, 2021
@suhyunsim suhyunsim added this to the 7월 2주 차 milestone Jul 7, 2021
@suhyunsim suhyunsim self-assigned this Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DP 다이나믹 프로그래밍 lv.2 프로그래머스 - level 2 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant