Finding the Root of a Tree (Finding the Common Destination)
- 时间:2020-09-09 14:04:20
- 分类:网络文摘
- 阅读:98 次
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Example 1:
Input: paths = [[“London”,”New York”],[“New York”,”Lima”],[“Lima”,”Sao Paulo”]]
Output: “Sao Paulo”
Explanation: Starting at “London” city you will reach “Sao Paulo” city which is the destination city. Your trip consist of: “London” -> “New York” -> “Lima” -> “Sao Paulo”.Example 2:
Input: paths = [[“B”,”C”],[“D”,”B”],[“C”,”A”]]
Output: “A”
Explanation: All possible trips are:
“D” – “B” – “C” – “A”.
“B” – “C” – “A”.
“C” – “A”.
“A”.
Clearly the destination city is “A”.Example 3:
Input: paths = [[“A”,”Z”]]
Output: “Z”Constraints:
1 <= paths.length <= 100
paths[i].length == 2
1 <= cityAi.length, cityBi.length <= 10
cityAi != cityBi
All strings consist of lowercase and uppercase English letters and the space character.Hints:
Start in any city and use the path to move to the next city.
Eventually, you will reach a city with no path outgoing, this is the destination city.
Finding the Root of the Tree
Finding the common destination is equivalent as finding the root of (common ancestor) of the tree. The given input contains a list of a line segment that we can use to construct the tree. Then, we start from any of the node, iteratively traversing until we can’t go further, which is guaranteed to be the root.
The following C++ uses a hash map to store the paths between nodes. The complexity is O(N). In the best case, it only needs to go through the paths once, and in the worst case, twice (the longest path).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_map<string, string> data; for (const auto &n: paths) { data[n[0]] = n[1]; } string city = paths[0][0]; while (data.find(city) != data.end()) { city = data[city]; } return city; } }; |
class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_map<string, string> data; for (const auto &n: paths) { data[n[0]] = n[1]; } string city = paths[0][0]; while (data.find(city) != data.end()) { city = data[city]; } return city; } };
Another algorithm is to push all the nodes into a set e.g. unordered_set. And the second time, we remove the start from the set, and remaining node in the set must be the destination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_set<string> cities; for (const auto &n: paths) { cities.insert(n[0]); cities.insert(n[1]); } for (const auto &n: paths) { cities.erase(n[0]); } return *begin(cities); } }; |
class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_set<string> cities; for (const auto &n: paths) { cities.insert(n[0]); cities.insert(n[1]); } for (const auto &n: paths) { cities.erase(n[0]); } return *begin(cities); } };
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:数学题:如何按照一定的比例把小长方形扩大成与大长方形完全重的图形 问答题:说明学生总数、每辆车载客数、客车数成什么比例 数学题:有一个礼品盒,用彩绳扎成如右图的形状 数学题:客车从甲地到乙地要6小时;货车从乙地到甲地要8小时 数学题:一件商品按成本提高30%,换季又打八折 数学题:前三轮的平均的平均分是94 数学题:财务室会计结账时,发现账面上少了890.1元钱 数学题:一个玻璃瓶内原有盐是水的1/11 数学题:把圆柱平均分成若干份后拼成一个长方体 奥数题:甲乙两地中间有一座山岭
- 评论列表
-
- 添加评论