-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarpet.java
43 lines (37 loc) · 1.03 KB
/
carpet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public int[] solution(int brown, int red) {
int[] answer = new int[2];
int n = brown + red;
for(int i = 3; i*i <= n ; i++){
if(n % i == 0){
int x = n/i;
int y = i;
if(check(x, y, red)){
answer[0] = x;
answer[1] = y;
break;
}
}
}
return answer;
}
public boolean check(int x, int y, int red){
boolean flag = false;
boolean anyway_no = false;
for(int i = 2; x - i >= 1 ; i += 2){
int i_tmp = x-i;
for(int j = 2; y - j >= 1; j += 2){
int tmp = i_tmp*(y-j);
if(tmp == red) {
flag = true;
break;
} else if( tmp < red) {
anyway_no = true;
break;
}
}
if(flag || anyway_no) break;
}
return flag;
}
}