Find negative degree | Video Solution | Java | C++ | Explanation With Drawings
Intuition and approach dicussed in detail in video solution.\nhttps://youtu.be/wQzVsmakH-A\n\n# Code\nC++\n\nclass Solution {\npublic:\n    string destCity(vect
# Intuition and approach dicussed in detail in video solution.\nhttps://youtu.be/wQzVsmakH-A\n\n# Code\nC++\n```\nclass Solution {\npublic:\n    string destCity(vector<vector<string>>& paths) {\n        unordered_map<string, int> cityDegreeMap;\n        for(auto &path : paths){\n            //for outward edge from a city give increament of one to the degree of city\n            cityDegreeMap[path[0]]++;\n            //for inward edge towards a city give decreament of minus one to the degree of city\n            cityDegreeMap[path[1]]--;\n        }\n        //traverse you degree map to find out the city with -1 as the degree\n        for(auto &cityDegPair : cityDegreeMap){\n            if(cityDegPair.second == -1)return cityDegPair.first;\n        }\n        return "";\n    }\n};\n```\n\nJava\n```\nclass Solution {\n    public String destCity(List<List<String>> paths) {\n        Map<String, Integer> cityDegreeMap = new HashMap<>();\n        for(var path : paths){\n            //for outward edge from a city give increament of one to the degree of city\n            cityDegreeMap.put(path.get(0), cityDegreeMap.getOrDefault(path.get(0), 0) + 1);\n            cityDegreeMap.put(path.get(1), cityDegreeMap.getOrDefault(path.get(1), 0) - 1);\n        }\n        //traverse you degree map to find out the city with -1 as the degree\n        for(var cityDegPair : cityDegreeMap.entrySet()){\n            if(cityDegPair.getValue() == -1)return cityDegPair.getKey();\n        }\n        return "";\n    }\n}\n```
['C++', 'Java']