Loading Similar Posts
Problem description is confusing. I am not sure if I understood the problem correctly.
But here's my observation:
- F is always N because the child can run at speed 1 and cover every parent's steps
- So the problem scope is reduced to find maximum V2 to cover all parent's steps
- If X1 % V1 == X2 % V1 then the child can run at speed V1 and cover every parent's steps
- Otherwise, the child can run at speed gcd(V1, X1 %V1, X2 % V1) to cover every parent's steps
class Solution:
def calculateVelocity(self, parentPos: int, childPos: int, velParent: int, steps: int) -> [int, int]:
if parentPos % velParent == childPos % velParent:
return [steps, velParent]
return [steps, math.gcd(velParent, parentPos % velParent, childPos % velParent)]
Complexity is both O(1)