当前位置:网站首页>2022-02-15 (399. Division evaluation)

2022-02-15 (399. Division evaluation)

2022-07-01 04:30:00 TickTick123

class Solution {
    
    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
    
        int equationsSize = equations.size();

        UnionFind unionFind = new UnionFind(2 * equationsSize);
        //  The first  1  Step : Preprocessing , Compare the value of the variable with  id  mapping , Make the bottom layer of the parallel search set use array to realize , Easy to code 
        Map<String, Integer> hashMap = new HashMap<>(2 * equationsSize);
        int id = 0;
        for (int i = 0; i < equationsSize; i++) {
    
            List<String> equation = equations.get(i);
            String var1 = equation.get(0);
            String var2 = equation.get(1);

            if (!hashMap.containsKey(var1)) {
    
                hashMap.put(var1, id);
                id++;
            }
            if (!hashMap.containsKey(var2)) {
    
                hashMap.put(var2, id);
                id++;
            }
            unionFind.union(hashMap.get(var1), hashMap.get(var2), values[i]);
        }

        //  The first  2  Step : Make a query 
        int queriesSize = queries.size();
        double[] res = new double[queriesSize];
        for (int i = 0; i < queriesSize; i++) {
    
            String var1 = queries.get(i).get(0);
            String var2 = queries.get(i).get(1);

            Integer id1 = hashMap.get(var1);
            Integer id2 = hashMap.get(var2);

            if (id1 == null || id2 == null) {
    
                res[i] = -1.0d;
            } else {
    
                res[i] = unionFind.isConnected(id1, id2);
            }
        }
        return res;
    }

    private class UnionFind {
    

        private int[] parent;

        /** *  The weight of the parent node pointed to  */
        private double[] weight;


        public UnionFind(int n) {
    
            this.parent = new int[n];
            this.weight = new double[n];
            for (int i = 0; i < n; i++) {
    
                parent[i] = i;
                weight[i] = 1.0d;
            }
        }

        public void union(int x, int y, double value) {
    
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
    
                return;
            }

            parent[rootX] = rootY;
          	//  For the derivation of the relation, see 「 Reference code 」 Diagram below 
            weight[rootX] = weight[y] * value / weight[x];
        }

        /** *  Path compression  * * @param x * @return  Of the root node  id */
        public int find(int x) {
    
            if (x != parent[x]) {
    
                int origin = parent[x];
                parent[x] = find(parent[x]);
                weight[x] *= weight[origin];
            }
            return parent[x];
        }

        public double isConnected(int x, int y) {
    
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
    
                return weight[x] / weight[y];
            } else {
    
                return -1.0d;
            }
        }        
    }
}

Alas, my life sighs , The priority of joint search set is low

原网站

版权声明
本文为[TickTick123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160256058605.html