399. Evaluate Division Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Union Find#Graph#Shortest Path



1/**
2 * [399] Evaluate Division
3 *
4 * 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.
5 * You are also given some queries, where queries[j] = [Cj, Dj] represents the j^th query where you must find the answer for Cj / Dj = ?.
6 * Return the answers to all queries. If a single answer cannot be determined, return -1.0.
7 * 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.
8 *  
9 * Example 1:
10 * 
11 * Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
12 * Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
13 * Explanation: 
14 * Given: a / b = 2.0, b / c = 3.0
15 * queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
16 * return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
17 * 
18 * Example 2:
19 * 
20 * Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
21 * Output: [3.75000,0.40000,5.00000,0.20000]
22 * 
23 * Example 3:
24 * 
25 * Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
26 * Output: [0.50000,2.00000,-1.00000,-1.00000]
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= equations.length <= 20
32 * 	equations[i].length == 2
33 * 	1 <= Ai.length, Bi.length <= 5
34 * 	values.length == equations.length
35 * 	0.0 < values[i] <= 20.0
36 * 	1 <= queries.length <= 20
37 * 	queries[i].length == 2
38 * 	1 <= Cj.length, Dj.length <= 5
39 * 	Ai, Bi, Cj, Dj consist of lower case English letters and digits.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/evaluate-division/
45// discuss: https://leetcode.com/problems/evaluate-division/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn calc_equation(equations: Vec<Vec<String>>, values: Vec<f64>, queries: Vec<Vec<String>>) -> Vec<f64> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_399() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.