💻 문제 출처 : 프로그래머스_카펫
class Solution {
public int[] solution(int brown, int yellow) {
int x = 0;
int y = 0;
int area = brown + yellow;
for(int i = 1; i <= area / 2; i++) {
if(area % i == 0) {
x = i >= area / i ? i : area / i;
y = i < area / i ? i : area / i;
}
if((x - 2) * (y - 2) == yellow) break;
}
return new int[]{x, y};
}
}
class Solution {
public int[] solution(int brown, int yellow) {
int totalCells = brown + yellow;
for (int height = 3; height <= (int)Math.sqrt(totalCells); height++) {
if (totalCells % height == 0) {
int width = totalCells / height;
if (brown == (width * 2) + (height * 2) - 4) {
return new int[]{width, height};
}
}
}
return new int[]{0, 0};
}
}
class Solution {
public int[] solution(int brown, int yellow) {
for(int i=1; i<=yellow; i++) {
if(yellow%i==0 && (yellow/i+i)*2+4==brown) {
return new int[] {yellow/i+2, i+2};
}
}
return null;
}
}