- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/340198
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/PCCE 기출문제 10번. 공원
풀이 시간 : 38분
import java.util.*;
class Solution {
public int solution(int[] mats, String[][] park) {
int rows = park.length;
int cols = park[0].length;
Arrays.sort(mats);
for (int m = mats.length - 1; m >= 0; m--) {
int size = mats[m];
if (canPlaceMat(size, park, rows, cols)) {
return size;
}
}
return -1;
}
public boolean canPlaceMat(int size, String[][] park, int rows, int cols) {
for (int i = 0; i <= rows - size; i++) {
for (int j = 0; j <= cols - size; j++) {
if (checkArea(i, j, size, park)) {
return true;
}
}
}
return false;
}
public boolean checkArea(int startX, int startY, int size, String[][] park) {
for (int x = startX; x < startX + size; x++) {
for (int y = startY; y < startY + size; y++) {
if (!park[x][y].equals("-1")) {
return false;
}
}
}
return true;
}
}