https://school.programmers.co.kr/learn/courses/30/lessons/12905
λ¬Έμ μ€λͺ
1μ 0λ‘ μ±μμ§ ν(board)κ° μμ΅λλ€. ν 1μΉΈμ 1 x 1 μ μ μ¬κ°νμΌλ‘ μ΄λ£¨μ΄μ Έ μμ΅λλ€. νμμ 1λ‘ μ΄λ£¨μ΄μ§ κ°μ₯ ν° μ μ¬κ°νμ μ°Ύμ λμ΄λ₯Ό return νλ solution ν¨μλ₯Ό μμ±ν΄ μ£ΌμΈμ. (λ¨, μ μ¬κ°νμ΄λ μΆμ ννν μ μ¬κ°νμ λ§ν©λλ€.)
μλ₯Ό λ€μ΄
κ° μλ€λ©΄ κ°μ₯ ν° μ μ¬κ°νμ
κ° λλ©° λμ΄λ 9κ° λλ―λ‘ 9λ₯Ό λ°νν΄ μ£Όλ©΄ λ©λλ€.
λ΄ νμ΄ 1
μ«μ 1μΌλλ§λ€ μ΅λ ν¬κΈ°λ₯Ό κ²μνλ€.
#include <iostream>
#include<vector>
using namespace std;
vector<vector<int>> Board;
int CheckSize(int row, int col)
{
int size = 0;
for (int s = 0; s < Board.size(); ++s)
{
bool isSquare = true;
if (row + s >= Board.size() || col + s >= Board[0].size())
break;
for (int n = 0; n <= s; ++n)
{
if (0 == Board[row + n][col + s] || 0 == Board[s + row][col + n])
{
isSquare = false;
break;
}
}
if (!isSquare)
break;
size = s;
}
return size+1;
}
int solution(vector<vector<int>> board)
{
int answer = 0;
Board = board;
for (int r = 0; r < board.size(); ++r)
for (int c = 0; c < board[0].size(); ++c)
{
if (board[r][c] == 1)
{
if (r + answer >= board.size() || c + answer >= board[0].size())
continue;
answer = max(CheckSize(r, c), answer);
}
}
answer *= answer;
return answer;
}
μ ννμ§λ§ μκ° λ³΅μ‘λμμ μ€ν¨νλ€.
λ΄ νμ΄ 2
DP
μ νμ
board[i][j] = 1 + min({board[i-1][j-1], board[i-1][j], board[i][j-1]});
νμ¬ μμΉμμ μ’μ, μ’, μ μμ μ€μ κ°μ₯ μμ μ+1μ ν΄λκ°λ©΄μ μ΅λ ν¬κΈ°μ μ μ¬κ°νμ μ°Ύμ μ μλ€.
μ΄λ° μμΌλ‘ 1, 1λΆν° μ μ§ννλ€λ³΄λ©΄ λ€μκ³Ό κ°μ λͺ¨μ΅μ΄ λλ€.
μ΄μ€μμ κ°μ₯ ν° κ°μ΄ μ΅λ ν¬κΈ°μ μ μ¬κ°νμ΄λ€.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<vector<int>> board)
{
int answer = board[0][0];
int r = board.size();
int c = board[0].size();
for(int i=1;i<r;i++)
{
for(int j=1;j<c;j++)
{
if(board[i][j]==1){
board[i][j] = 1 + min({board[i-1][j-1],board[i-1][j],board[i][j-1]});
answer = max(answer,board[i][j]);
}
}
}
return answer*answer;
}
'π₯οΈ Study Note > Coding Test' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€]level.3 - μΉ΄λ μ§ λ§μΆκΈ°(C++) (1) | 2023.10.05 |
---|---|
[νλ‘κ·Έλλ¨Έμ€]level.3 - λΆλλ³΅κ· (C++) (0) | 2023.10.04 |
[νλ‘κ·Έλλ¨Έμ€]level.3 - λ€λ¨κ³ μΉ«μ ν맀 (C++) (0) | 2023.09.26 |
[νλ‘κ·Έλλ¨Έμ€]level.2 - μ«μ λ³ννκΈ° (C++) (0) | 2023.09.22 |
[νλ‘κ·Έλλ¨Έμ€]level.2 - λ°©λ¬Έ κΈΈμ΄ (C++) (0) | 2023.09.21 |