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.
Minimum Penalty Path
Minimum Penalty Path
Sort by
recency
|
57 Discussions
|
Please Login in order to post a comment
Golang solution just in case someone is looking. NOTE: Dijkstra will not work as explained [here]https://www.hackerrank.com/challenges/beautiful-path/forum/comments/121506). We need to go with brute force as input size is small, so a simple dfs/bfs needed to check all possible distances of a vertex from starting vertex(in this case 'A').
Here is my solution in java, javascript, python, C, C++, csharp HackerRank Minimum Penalty Path Solution
Hi There! i want to use it for my Gaming Website .
Here is the solution of Minimum Penalty Path Click Here
It can be solved pretty easily by using brute force since the maximum edge cost is pretty small (1023).
The key observation to make is that the answer lies between 1 and 1023, if there is a path from A to B, otherwise -1. So you can just test if it's possible to get from A to B with a cost of 1, 2, 3, ... up to 1023. So all that is left is to create a graph where the OR of all its edges results to the cost you are testing for.
I used a disjoinct set data structure to see if A and B are connected in the resulting graph but I'm pretty sure just doing a DFS from A to see if you can reach B works too.