๐Ÿ”ฅ[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋””] 25์ผ์ฐจ TIL - Evaluate Division

HOONSSACยท2024๋…„ 8์›” 15์ผ
1

99Club Coding Test Study

๋ชฉ๋ก ๋ณด๊ธฐ
25/41
post-thumbnail
post-custom-banner

โณ๋ฌธ์ œ

You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.

๐Ÿšจ์ œํ•œ ์‚ฌํ•ญ

  • 1 <= equations.length <= 20
  • equations[i].length == 2
  • 1 <= Ai.length, Bi.length <= 5
  • values.length == equations.length
  • 0.0 < values[i] <= 20.0
  • 1 <= queries.length <= 20
  • queries[i].length == 2
  • 1 <= Cj.length, Dj.length <= 5
  • Ai, Bi, Cj, Dj consist of lower case English letters and digits.

๐Ÿ“„์ž…์ถœ๋ ฅ ์˜ˆ

์ž…์ถœ๋ ฅ ์˜ˆ #1

์ž…๋ ฅ

equationsvaluesqueries
[["a","b"],["b","c"]][2.0,3.0][["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]

์ถœ๋ ฅ

output
[6.00000,0.50000,-1.00000,1.00000,-1.00000]

์„ค๋ช…
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
note: x is undefined => -1.0


์ž…์ถœ๋ ฅ ์˜ˆ #2

์ž…๋ ฅ

equationsvaluesqueries
[["a","b"],["b","c"],["bc","cd"]][1.5,2.5,5.0][["a","c"],["c","b"],["bc","cd"],["cd","bc"]]

์ถœ๋ ฅ

output
[3.75000,0.40000,5.00000,0.20000]

์ž…์ถœ๋ ฅ ์˜ˆ #3

์ž…๋ ฅ

equationsvaluesqueries
[["a","b"]][0.5][["a","b"],["b","a"],["a","c"],["x","y"]]

์ถœ๋ ฅ

output
[0.50000,2.00000,-1.00000,-1.00000]

โœ๏ธํ’€์ด

๋ฌธ์ œ ์„ค๋ช…๋งŒ์œผ๋กœ๋Š” ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋ผ์„œ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์•˜๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #1์„ ์‚ดํŽด๋ณด๋ฉด,
["a","b"]์˜ value๋Š” 2.0
["b","c"]์˜ value๋Š” 3.0์œผ๋กœ ์ฃผ์–ด์กŒ๋‹ค.

์ด๋Š” ์•„๋ž˜์˜ ๋‘ ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  • a / b = 2.0
  • b / c = 3.0

๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ์˜ queries ์ค‘ ["a","c"]์— ํ•ด๋‹นํ•˜๋Š” ์ถœ๋ ฅ์œผ๋กœ, a / c๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
์ด ๋•Œ, a / c๋Š” a / b์™€ b / c๋ฅผ ๊ณฑํ•œ ๊ฐ’์œผ๋กœ, 6.0์ด ์ถœ๋ ฅ๋œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด queries ์ค‘ ["b", "a"]์˜ ์ถœ๋ ฅ๊ฐ’์€ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

๋ฐ”๋กœ a / b์˜ ์—ญ์ˆ˜์ธ 0.5๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ˜•์„ฑ๋œ ๊ด€๊ณ„๋ฅผ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ€์ค‘์น˜ ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ด์ œ, ์ด ๊ทธ๋ž˜ํ”„์—์„œ ์›ํ•˜๋Š” ์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด ๊ฒฝ๋กœ ์ƒ์— ์žˆ๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด ์›ํ•˜๋Š” ์ถœ๋ ฅ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค!

์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋Š” DFS๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

๐Ÿ‘พ์ตœ์ข… ์ฝ”๋“œ

class Solution {

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        // ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
        Map<String, Map<String, Double>> graph = new HashMap<>();

        // ๊ทธ๋ž˜ํ”„ ์ž…๋ ฅ
        for (int i = 0; i < equations.size(); i++) {
            String start = equations.get(i).get(0); // ์ถœ๋ฐœ์ง€
            String dest = equations.get(i).get(1); // ๋„์ฐฉ์ง€
            double value = values[i]; // ๊ฐ€์ค‘์น˜

			// ์ถœ๋ฐœ์ง€๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋„์ฐฉ์ง€์™€ ๊ฐ€์ค‘์น˜ ์ž…๋ ฅ
            graph.computeIfAbsent(start, k -> new HashMap<>()).put(dest, value);
            // ๋„์ฐฉ์ง€๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ถœ๋ฐœ์ง€์™€ ๊ฐ€์ค‘์น˜์˜ ์—ญ์ˆ˜๋ฅผ ์ž…๋ ฅ
            graph.computeIfAbsent(dest, k -> new HashMap<>()).put(start, 1 / value);
        }
        
        double[] answer = new double[queries.size()]; // ์ตœ์ข… ์ถœ๋ ฅ ๋ฐฐ์—ด

        for (int i = 0; i < queries.size(); i++) {
            String start = queries.get(i).get(0); // queries์˜ ์ถœ๋ฐœ์ง€ ์ถ”์ถœ
            String dest = queries.get(i).get(1); // queries์˜ ๋„์ฐฉ์ง€ ์ถ”์ถœ
            Set<String> visited = new HashSet<>(); // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ Set ์ƒ์„ฑ

            answer[i] = dfs(graph, start, dest, visited); // dfs ํ˜ธ์ถœ ํ›„ ๋ฐ˜ํ™˜๊ฐ’ ์ €์žฅ
        }

        return answer; // ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜

    }
    
    // ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
    public double dfs(Map<String, Map<String, Double>> graph, String start, String dest, Set<String> visited) {
        if (!graph.containsKey(start)) {
            return -1.0; // ๋งŒ์•ฝ ์ถœ๋ฐœ์ง€๊ฐ€ ๊ทธ๋ž˜ํ”„์— ์—†์œผ๋ฉด -1 ๋ฐ˜ํ™˜
        }
        if (start.equals(dest)) {
            return 1.0; // ์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€๊ฐ€ ๊ฐ™์œผ๋ฉด 1 ๋ฐ˜ํ™˜
        }
        visited.add(start); // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ํ‘œ์‹œ

		// ์ •์ ์— ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ด์›ƒ ์ •์ ๊ณผ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ ธ์˜ด
        for (Map.Entry<String, Double> neighbor : graph.get(start).entrySet()) {
        	// ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ
            if (!visited.contains(neighbor.getKey())) {
            	// ํ˜„์žฌ ์ •์ ์—์„œ ๋„์ฐฉ์ง€๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด dfs ์žฌ๊ท€ ํ˜ธ์ถœ
                double result = dfs(graph, neighbor.getKey(), dest, visited);
                // ๋„์ฐฉ์ง€์— ๋„๋‹ฌํ•œ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋ฐœ๊ฒฌ๋œ ๊ฒฝ์šฐ
                if (result != -1.0) {
                	// ํ•ด๋‹น ๊ฒฝ๋กœ์˜ ๊ฐ€์ค‘์น˜์— ํ˜„์žฌ ์ด์›ƒ ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๊ณฑํ•˜์—ฌ ๋ฐ˜ํ™˜
                    return result * neighbor.getValue();
                }
            }
        }
        return -1.0; // ๊ฒฝ๋กœ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์„ ๊ฒฝ์šฐ -1.0 ๋ฐ˜ํ™˜
    }
}

์ •์ ์— ์—ฐ๊ฒฐ๋œ ์ด์›ƒ ์ •์ ๋“ค์„ ํƒ์ƒ‰ํ•  ๋•Œ, Map.Entry๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ Map์— ์ €์žฅ๋œ ๋ชจ๋“  key-value ์Œ์„ ๊ฐ์˜ key-value๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์—ˆ๋‹ค.


๐Ÿ”—๋ฌธ์ œ ๋งํฌ
๐Ÿ’ปRepository

profile
ํ›ˆ์‹น์˜ ๊ฐœ๋ฐœ์—ฌํ–‰
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 8์›” 15์ผ

์–ด๋ ค์šด ๋ฌธ์ œ๊ตฐ์š”...! ์˜ค๋Š˜๋„ ๊ณ ์ƒ์ด ๋งŽ์•„์š”โ˜บ๏ธ ๋งˆ์ง€๋ง‰์— ํƒœ๊ทน๊ธฐ ์„ผ์Šค๐Ÿ‘

1๊ฐœ์˜ ๋‹ต๊ธ€