Skip to content

Commit 56fcf4c

Browse files
committed
[level 2] Title: 가장 큰 정사각형 찾기, Time: 21.19 ms, Memory: 90 MB -BaekjoonHub
1 parent 1dd2d30 commit 56fcf4c

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# [level 2] 가장 큰 정사각형 찾기 - 12905
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12905)
4+
5+
### 성능 요약
6+
7+
메모리: 90 MB, 시간: 21.19 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 59.6<br/>효율성: 40.4<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 08월 28일 21:10:28
20+
21+
### 문제 설명
22+
23+
<p>1와 0로 채워진 표(board)가 있습니다. 표 1칸은 1 x 1 의 정사각형으로 이루어져 있습니다. 표에서 1로 이루어진 가장 큰 정사각형을 찾아 넓이를 return 하는 solution 함수를 완성해 주세요. (단, 정사각형이란 축에 평행한 정사각형을 말합니다.)</p>
24+
25+
<p>예를 들어</p>
26+
<table class="table">
27+
<thead><tr>
28+
<th style="text-align: center">1</th>
29+
<th style="text-align: center">2</th>
30+
<th style="text-align: center">3</th>
31+
<th style="text-align: center">4</th>
32+
</tr>
33+
</thead>
34+
<tbody><tr>
35+
<td style="text-align: center">0</td>
36+
<td style="text-align: center">1</td>
37+
<td style="text-align: center">1</td>
38+
<td style="text-align: center">1</td>
39+
</tr>
40+
<tr>
41+
<td style="text-align: center">1</td>
42+
<td style="text-align: center">1</td>
43+
<td style="text-align: center">1</td>
44+
<td style="text-align: center">1</td>
45+
</tr>
46+
<tr>
47+
<td style="text-align: center">1</td>
48+
<td style="text-align: center">1</td>
49+
<td style="text-align: center">1</td>
50+
<td style="text-align: center">1</td>
51+
</tr>
52+
<tr>
53+
<td style="text-align: center">0</td>
54+
<td style="text-align: center">0</td>
55+
<td style="text-align: center">1</td>
56+
<td style="text-align: center">0</td>
57+
</tr>
58+
</tbody>
59+
</table>
60+
<p>가 있다면 가장 큰 정사각형은</p>
61+
<table class="table">
62+
<thead><tr>
63+
<th style="text-align: center">1</th>
64+
<th style="text-align: center">2</th>
65+
<th style="text-align: center">3</th>
66+
<th style="text-align: center">4</th>
67+
</tr>
68+
</thead>
69+
<tbody><tr>
70+
<td style="text-align: center">0</td>
71+
<td style="text-align: center"><code>1</code></td>
72+
<td style="text-align: center"><code>1</code></td>
73+
<td style="text-align: center"><code>1</code></td>
74+
</tr>
75+
<tr>
76+
<td style="text-align: center">1</td>
77+
<td style="text-align: center"><code>1</code></td>
78+
<td style="text-align: center"><code>1</code></td>
79+
<td style="text-align: center"><code>1</code></td>
80+
</tr>
81+
<tr>
82+
<td style="text-align: center">1</td>
83+
<td style="text-align: center"><code>1</code></td>
84+
<td style="text-align: center"><code>1</code></td>
85+
<td style="text-align: center"><code>1</code></td>
86+
</tr>
87+
<tr>
88+
<td style="text-align: center">0</td>
89+
<td style="text-align: center">0</td>
90+
<td style="text-align: center">1</td>
91+
<td style="text-align: center">0</td>
92+
</tr>
93+
</tbody>
94+
</table>
95+
<p>가 되며 넓이는 9가 되므로 9를 반환해 주면 됩니다.</p>
96+
97+
<h5>제한사항</h5>
98+
99+
<ul>
100+
<li>표(board)는 2차원 배열로 주어집니다.</li>
101+
<li>표(board)의 행(row)의 크기 : 1,000 이하의 자연수</li>
102+
<li>표(board)의 열(column)의 크기 : 1,000 이하의 자연수</li>
103+
<li>표(board)의 값은 1또는 0으로만 이루어져 있습니다.</li>
104+
</ul>
105+
106+
<hr>
107+
108+
<h5>입출력 예</h5>
109+
<table class="table">
110+
<thead><tr>
111+
<th>board</th>
112+
<th>answer</th>
113+
</tr>
114+
</thead>
115+
<tbody><tr>
116+
<td>[[0,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,1,0]]</td>
117+
<td>9</td>
118+
</tr>
119+
<tr>
120+
<td>[[0,0,1,1],[1,1,1,1]]</td>
121+
<td>4</td>
122+
</tr>
123+
</tbody>
124+
</table>
125+
<h5>입출력 예 설명</h5>
126+
127+
<p>입출력 예 #1<br>
128+
위의 예시와 같습니다.</p>
129+
130+
<p>입출력 예 #2</p>
131+
132+
<p>| 0 | 0 | <code>1</code> | <code>1</code> |<br>
133+
| 1 | 1 | <code>1</code> | <code>1</code> | <br>
134+
로 가장 큰 정사각형의 넓이는 4가 되므로 4를 return합니다.</p>
135+
136+
137+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
class Solution {
3+
int[][] dp;
4+
int[] dr ={-1,0,-1};
5+
int[] dc ={0,-1,-1};
6+
7+
public int solution(int[][] board) {
8+
int row = board.length;
9+
int col = board[0].length;
10+
dp = new int[row][col];
11+
int answer = 0;
12+
13+
for(int i =0 ; i<row; i++){
14+
for (int j = 0; j<col ; j++){
15+
int num = board[i][j];
16+
if(num == 1) {
17+
int number = getDp(board,i,j);
18+
// 최댓값 갱신
19+
if(answer < number) answer = number;
20+
}
21+
}
22+
}
23+
24+
// 넓이
25+
return (int) Math.pow(answer,2);
26+
}
27+
28+
int getDp(int[][] board,int row,int col){
29+
int neighborRow;
30+
int neighborCol;
31+
int min = Integer.MAX_VALUE;
32+
33+
for(int i = 0; i<3;i++){
34+
neighborRow = row + dr[i];
35+
neighborCol = col + dc[i];
36+
37+
// 범위를 벗어나면
38+
if(neighborRow < 0 || neighborCol < 0){
39+
min = 0;
40+
} else{
41+
// 최솟값 갱신
42+
min = Math.min(min, dp[neighborRow][neighborCol]);
43+
}
44+
}
45+
46+
// 근처 숫자들의 최솟값 + 1
47+
dp[row][col] = min + 1;
48+
return min+1;
49+
}
50+
}

0 commit comments

Comments
 (0)