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.
Longest Mod Path
Longest Mod Path
Sort by
recency
|
8 Discussions
|
Please Login in order to post a comment
Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Longest Mod Path Problem Solution
Here is Longest Mod Path problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-longest-mod-path-problem-solution.html
My solution, in Javascript: I've used a sorted list for the corridors, and then an "extract" method in order to get all the following corridors to a room using binary search. Then finding the distance from the first room (A) to all the other rooms in the maze. After that, a simple substraction to find all the queried distances: from-X-to-Y equals from-A-to-Y minus from-A-to-X. Since all the rooms are connected (by definition of the problem), any loop found will affect all the distances the same. And the sum of loops equals the GDP between them. Finally the closest you can be to the MOD value is the residue of the difference between your found distance and the MOD value minus one applying the GDP between the maze loop and the given MOD.
Haskell Solution
若存在三个环的周长分别是A, B, C,对A、B、C求公约数P,在环A,B,C分别走了i、j、k圈的情况下,走的总距离为i*A + j * B + k * C = P * l 。这里面i, j, k, l都是变量,必定可以调整 i、j、k、 l 的值使 l = n * m + 1,这时 i*A + j * B + k * C = (p * l) % m = p ,每重复一次该(i, j, k, l)数值组合的循环,余数能得到最小的增长p。所以,从点 S 到点 R ,只需求找出其中一条路径的距离,然后让余数按p增长,终能达到最大的余数值。
Find rings, get circumferences value of rings, than calculate common divisor for them.