Java easy to understand solution beat 75% like city connection question


  • 0
    G
    public class Solution {
        	public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
            HashMap<String,Integer> map =new HashMap<>();
            int secNo = 0;
            
            HashMap<String,Double> map1 =new HashMap<>();
            for(int i=0;i<values.length;i++){
                String tp1 = equations[i][0];
                String tp2 = equations[i][1];
                double tp3 = values[i];
                if(!map1.containsKey(tp1) && !map1.containsKey(tp2)){
                    map1.put(tp1,tp3);
                    map1.put(tp2,1.0);
                    
                    map.put(tp1,secNo);
                    map.put(tp2,secNo);
                    secNo++;
                }
                else if(map1.containsKey(tp1) && !map1.containsKey(tp2)){
                    double tempN = map1.get(tp1);
                    map1.put(tp2,tempN/tp3);
                    
                    map.put(tp2,map.get(tp1));
                }
                else if(!map1.containsKey(tp1) && map1.containsKey(tp2)){
                    double tempN = map1.get(tp2);
                    map1.put(tp1,tempN*tp3);
                    
                    map.put(tp1,map.get(tp2));
                }
                else if(map1.containsKey(tp1) && map1.containsKey(tp2))
                 {
                     double db1 = map1.get(tp1);
                     double db2 = map1.get(tp2);
                     double rate = tp3*db2/db1;
                     for(String key : map.keySet()){
                         if(map.get(key)==map.get(tp1)){
                             double tempN = map1.get(key);
                             map1.put(key,tempN*rate);
                         }
                     }
                     
                    int au = map.get(tp1);
                    int bu = map.get(tp2);
                    if(au!=bu){
                        for(String key:map.keySet()){
                            if(map.get(key)==bu){
                                map.put(key,au);
                            }
                        }
                    }
                     
                 }
            }
            int len1 = queries.length;
            double[] result = new double[len1];
            for(int i=0;i<len1;i++){
                String tp1 = queries[i][0];
                String tp2 = queries[i][1];
                if(!map.containsKey(tp1) || !map.containsKey(tp2) || map.get(tp1)!=map.get(tp2)){
                    result[i]=-1.0;
                }
                else{
                    double d1 = map1.get(tp1);
                    double d2 = map1.get(tp2);
                    result[i] = d1/d2;
                }
                
            }
            
            return result;
        }
        
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.