We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It took time to pass all tests (performance requirements). So for those who stuck and looking for hints (but not a full solution) - please continue reading. The challenge is interesting and it might be done with just two methods of 20-25 code-only lines. It might not be needed to use famous algorithms for shortest path, minimum spanning tree, traveling salesman, hamilton path. It might not be needed to build an additional graph with k nodes based on original one with n nodes. From the description - it's only single path between any two nodes. Further hints. Main goal is to calculate total distance between all k targets (which is a sum of all back-forward paths to\from target) excluding one "forward" path for start node and one "back" path for end node. Recursion (modification of dfs) can be used with just node 1 as a departure point. Cases when there are no target-nodes up or down the way from current position (some node during recursion) should be handled appropriatly.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jeanie's Route
You are viewing a single comment's thread. Return to all comments →
It took time to pass all tests (performance requirements). So for those who stuck and looking for hints (but not a full solution) - please continue reading. The challenge is interesting and it might be done with just two methods of 20-25 code-only lines. It might not be needed to use famous algorithms for shortest path, minimum spanning tree, traveling salesman, hamilton path. It might not be needed to build an additional graph with k nodes based on original one with n nodes. From the description - it's only single path between any two nodes. Further hints. Main goal is to calculate total distance between all k targets (which is a sum of all back-forward paths to\from target) excluding one "forward" path for start node and one "back" path for end node. Recursion (modification of dfs) can be used with just node 1 as a departure point. Cases when there are no target-nodes up or down the way from current position (some node during recursion) should be handled appropriatly.