[C++] 리트코드 221. Maximal Square

2024. 2. 16. 00:38·Algorithm/HackerRank, LeetCode
class Solution 
{
public:
    int maximalSquare(vector<vector<char>>& matrix) 
    {
        //최대 사각형의 너비를 구하라
        int result = 0;
        int dp[300][300]{0};

        for(int m = 0; m < matrix.size(); m++)
        {
            if(matrix[m][0] == '1')
            {
                result = 1;
                dp[m][0] = 1;
            }
        }
        for(int n = 0; n < matrix[0].size(); n++)
        {
            if(matrix[0][n] == '1')
            {
                result = 1;
                dp[0][n] = 1;
            }
        }

        for(int m = 1; m < matrix.size(); m++)
        {
            for(int n = 1; n < matrix[m].size(); n++)
            {
                if(matrix[m][n] == '0')
                {
                    continue;
                }

                dp[m][n] = min(min(dp[m - 1][n - 1], dp[m - 1][n]), dp[m][n - 1]) + 1;
                result = max(dp[m][n], result);
            }
        }

        return result * result;
    }
};

 

풀이

1. 최대로 커질 수 있는 정사각형을 찾는다.

2. 왼쪽대각선, 상, 하의 값중 가장 작은 값에 + 1을 해준다.

11

11 일경우 2가 되는 것!

2가 되면 2*2의 정사각형이 완성된다.

3. 가장 큰 수의 제곱이 답이 된다.

 

 

 

https://leetcode.com/problems/maximal-square/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

저작자표시 (새창열림)

'Algorithm > HackerRank, LeetCode' 카테고리의 다른 글

[C++] 리트코드 Count and Say  (0) 2024.02.26
[C++] 리트코드 279. Perfect Squares  (0) 2024.02.16
[C++] 해커랭크 Cut the Tree  (0) 2023.12.26
'Algorithm/HackerRank, LeetCode' 카테고리의 다른 글
  • [C++] 리트코드 Count and Say
  • [C++] 리트코드 279. Perfect Squares
  • [C++] 해커랭크 Cut the Tree
chanheess
chanheess
'왜' 그렇게 했는가?에 대한 생각으로 공부 및 작업의 저장관리
  • chanheess
    왜 그렇게 생각했는가?
    chanheess
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Backend Programming
      • Game Programming
        • Unreal
        • DirectX
      • C++
        • Memo
        • Basic
        • Effective Modern
      • Algorithm
        • Memo
        • Baekjoon
        • Programmers
        • HackerRank, LeetCode
      • Data Structure
      • Design Pattern
      • Etc
        • Memo
        • Daily Log
        • Book
  • 최근 글

  • 최근 댓글

  • 태그

    Java
    dfs
    백준
    티스토리챌린지
    JPA
    JWT
    위클리 챌린지
    프로그래머스
    dp
    알고리즘
    SpringSecurity
    spring
    오블완
    c++ 기초 플러스
  • hELLO· Designed By정상우.v4.10.0
chanheess
[C++] 리트코드 221. Maximal Square
상단으로

티스토리툴바