완전 탐색
import java.util.HashMap;
class Solution {
public int[] solution(int brown, int yellow) {
int[] answer = new int[2];
// 가로, 세로 저장
HashMap<Integer, Integer> hashMap = new HashMap();
// 1. 카페트의 가로 또는 세로로 나올 수 있는 모든 경우
int sum = brown + yellow;
for(int i=2; i<=(int)Math.sqrt(sum); i++) {
if(sum % i == 0) {
hashMap.put(sum / i, i);
}
}
// 2. 제공받은 격자의 수와 필요한 격자의 수가 일치하는지 확인
for (HashMap.Entry<Integer, Integer> entry : hashMap.entrySet()) {
int width = (int)Math.max(entry.getKey(), entry.getValue());
int height = (int)Math.min(entry.getKey(), entry.getValue());
System.out.println("width:" + width + ", height:" + height);
if(brown == width*2 + (height-2)*2
&& yellow == width*height - (width*2 + (height-2)*2)){
answer[0]= width;
answer[1]=height;
}
}
return answer;
}
}'solve' 카테고리의 다른 글
| [프로그래머스/그리디] Lv1. 체육복(자바) (0) | 2024.01.09 |
|---|---|
| [프로그래머스/완전탐색] Lv2. 소수 찾기 (자바) (0) | 2024.01.09 |
| [프로그래머스] Lv2. 피로도 (자바) (0) | 2024.01.09 |
| [백준 1018] 체스판 다시 칠하기 (자바) (0) | 2024.01.09 |
| [백준 2606] 바이러스 (자바) (1) | 2024.01.09 |