티스토리 뷰

PS/programmers

Level2) 카펫

kingsubin 2021. 7. 13. 20:15
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
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
 
public class Level2_카펫 {
    public static int[] solution(int brown, int yellow) {
        // yellow 의 약수 쌍을 구한다.
        // 약수 쌍을 돌며 가능한 경우 모두 수식에 맞춰 brown 값을 맞춰본다.
        // w >= h
        // yellow 블럭의 w, h 를 구하는거니 return 할때는 w+2, h+2 해준다.
 
        List<Integer> divisor = getDivisor(yellow);
        return findBrownCount(brown, divisor);
    }
 
    public static List<Integer> getDivisor(int yellow) {
        List<Integer> list = new ArrayList();
 
        for (int i = 1; i <= Math.sqrt(yellow); i++) {
            if (yellow % i == 0) {
                list.add(i);
                list.add(yellow/i);
            }
        }
 
        Collections.sort(list);
        return list;
    }
 
    public static int[] findBrownCount(int brown, List<Integer> divisor) {
        int x = 0;
        int y = divisor.size() - 1;
        while (true) {
            int w = divisor.get(x);
            int h = divisor.get(y);
            x++;
            y--;
 
            if (h > w) {
                continue;
            };
 
            int totalBrown = ((w + 2* 2+ (h * 2);
            if (totalBrown == brown) {
                return new int[] {w+2, h+2};
            }
        }
    }
 
    public static void main(String[] args) {
        int brown = 24;
        int yellow = 24;
        int[] solution = solution(brown, yellow);
        System.out.println(solution[0+ ", " + solution[1]);
    }
}
 
cs

'PS > programmers' 카테고리의 다른 글

MySQL 2레벨 다 풀었다.  (0) 2021.08.06
MySQL 1레벨 다 풀었다.  (0) 2021.08.06
Level2) H-Index  (0) 2021.05.04
Level2) 위장  (0) 2021.05.03
Level1) 내적  (0) 2021.05.02