📝 Descrição
You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).
- The first kangaroo starts at location
x1
and moves at a rate ofv1
meters per jump.- The second kangaroo starts at location
x2
and moves at a rate ofv2
meters per jump.You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.
📌 Exemplos
Examplo 1:
x1 = 2
v1 = 1
x2 = 1
v2 = 2
Depois de um pulo eles estarão em x=3
✅ Casos de teste
Caso 1
Entrada
0 3 4 2
Saída
YES
🚀 Como resolvi?
A posição final de um canguru pode ser obtida por . Para que dois cangurus se encontrem no mesmo lugar depois de pulos, temos de
Ajustando a equação
Resolvendo-a por
Como precisa ser um número inteiro, então devemos garantir que
Além disso, temos de garantir que o canguru que começa por último é mais veloz do que o que está a frente
e
🛠️ Implementação
def kangaroo(x1, v1, x2, v2):
if v1 == v2:
return "YES" if x1 == x2 else "NO"
if (x2 - x1) % (v1 - v2) == 0 and (v1 > v2):
return "YES"
return "NO"
🧠 O que aprendemos?
Nem todos os problemas contidos nas plataformas serão relacionados a DSA.