Home [Algorithm] Brute-force Search 3
Post
Cancel

[Algorithm] Brute-force Search 3

Brute-force Search


Task

Previous task1
Previous task2

3. 카펫 in Programmers (Level 2)

πŸ“Œ Problem

https://programmers.co.kr/learn/courses/30/lessons/42842

πŸ“Œ The point

Used recursion

πŸ“Œ Answer

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
class Solution {

    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2];
        
        int sum = brown + yellow; // 12
        int hori, verti = 0;
        
        // find the possibility of hori and verti
        // condition : hori >= verti -> so i--
        for (int i = sum / 2; i > 2; i--) {
            if (sum % i == 0) {
                hori = i;
                if (check(hori, brown, yellow)) {
                    answer[0] = hori;
                    answer[1] = sum / hori;
                    break;
                }
            }
        }
        return answer;
    }
    
    private boolean check(int hori, int brown, int yellow) {
        int y_hori = hori - 2;
        
        if (yellow % y_hori != 0) return false;
        
        return true;
    }
}
/**
[Question]
return { horizontal length, vertical length }

[Condition]
- 8 <= brown <= 5000
- 1 <= yellow <= 2,000,000
- horizontal length >= vertical length

[Algorithm]
- Brute-force search : dfs (recursion)

[Logic]
- the goal : get hori, verti
- it's okay only know one variable, hori or verti
- brown must be 2 * hori + (verti - 2)
- yello must be hori - 2
**/
This post is licensed under CC BY 4.0 by the author.

[Algorithm] Brute-force Search 2

[Algorithm] Greedy1

Comments powered by Disqus.