Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

img_4.png

์„ค๋ช…

์šฐ์„  ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” equations ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ํ•ญ๋ชฉ์—๋Š” ๋ฌธ์ž์—ด์ด ๋“ค์–ด์žˆ๊ณ , equations[i] = [a, b] ๋ผ๊ณ  ํ•˜๋ฉด values ๋ฐฐ์—ด์—๋Š” a / b = values[i] ๊ฐ’์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค. ์ฆ‰ b์— ๋Œ€ํ•œ a์˜ ๋น„์œจ์ด ์ฃผ์–ด์ง„๋‹ค.

์ผ๋‹จ ์ด ๋ฌธ์ œ ์œ ํ˜•์ด ๊ทธ๋ž˜ํ”„ ์ธ์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์–ด๋ ต๋‹ค.. ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด a / b = 2 ์ด๊ณ , b / c = 3 ์ผ ๋•Œ, a / c ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ 6 ์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋˜๋Š” ์‹์ด๋‹ค.

์ด ๋ฌธ์ œ ํ•ด์„์˜ ํ•ต์‹ฌ์€ a / b = 2 ์ด๋ฉด b / a = 1/2 ๋ผ๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค. ์ฆ‰ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉํ–ฅ์„ฑ ์žˆ๋Š” ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์™€ ๊ฐ™๋‹ค.

1
2
a  --2-->  b  --3-->  c
a <--1/2-- b <--1/3-- c 

๊ทธ๋ž˜ํ”„์— ๋…ธ๋“œ ์ •๋ณด๋งŒ ๋„ฃ์–ด์„œ๋Š” ์•ˆ๋˜๊ณ , ๊ฐ€์ค‘์น˜ ์ •๋ณด๋„ ๊ฐ™์ด ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๋…ธ๋“œ ์ด๋ฆ„์ด โ€œaโ€, โ€œbโ€ ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฌธ์ž๋กœ ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋กœ ์ฐพ๋Š”๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์ •์˜ ์‹œ int[][] ๋‚˜, ArrayList<Integer>[] ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ Map ์˜ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.

1
2
// {a={b=2.0}, b={a=0.5, c=3.0}, c={b=0.3333333333333333}} ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค
Map<String, Map<String, Double>> graph = new HashMap<>();

ํ’€์ด

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
public class Solution {
    // ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„
    private Map<String, Map<String, Double>> graph = new HashMap<>();

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        // ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌ์„ฑ
        buildGraph(equations, values);

        double[] result = new double[queries.size()];
        for (int i = 0; i < queries.size(); i++) {
            String numerator = queries.get(i).get(0);
            String denominator = queries.get(i).get(1);
            result[i] = dfs(numerator, denominator, new HashSet<>());
        }
        return result;
    }

    private void buildGraph(List<List<String>> equations, double[] values) {
        for (int i = 0; i < equations.size(); i++) {
            String numerator = equations.get(i).get(0);
            String denominator = equations.get(i).get(1);
            double value = values[i];

            graph.putIfAbsent(numerator, new HashMap<>());
            graph.putIfAbsent(denominator, new HashMap<>());

            graph.get(numerator).put(denominator, value);
            graph.get(denominator).put(numerator, 1.0 / value);
        }
    }

    private double dfs(String current, String target, Set<String> visited) {
        if (!graph.containsKey(current) || !graph.containsKey(target)) {
            return -1.0;
        }
        if (current.equals(target)) {
            return 1.0;
        }

        visited.add(current);

        Map<String, Double> neighbors = graph.get(current);
        for (String neighbor : neighbors.keySet()) {
            if (!visited.contains(neighbor)) {
                double result = dfs(neighbor, target, visited);
                if (result != -1.0) {
                    return result * neighbors.get(neighbor);
                }
            }
        }

        return -1.0;
    }
}

ํšŒ๊ณ 

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ์œ ํ˜•์„ ๋งŽ์ด ์ ‘ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ํŠนํžˆ ์™œ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ธ์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ณ , ๊ทธ๋ž˜ํ”„๋ฅผ ์„ ์–ธ ์‹œ ์–ด๋–ค ํ˜•ํƒœ๋กœ ๋งŒ๋“ค ๊ฒƒ์ธ์ง€๋„ ์ƒ๊ฐํ•ด ๋ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ