Number Line Jumps

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn number_line_jumps(x1: i32, v1: i32, x2: i32, v2: i32) -> String {
        //Time complexity: O(1)
        //Space complexity (ignoring input): O(1)
        if x1 == x2 {
            return "YES".to_string();
        }
        if v1 == v2 {
            return "NO".to_string();
        }
    
        let relative_speed = v2 - v1;
        let initial_distance = x1 - x2;
        let jumps = initial_distance as f32 / relative_speed as f32;
        if jumps == jumps.abs().floor() {
            return "YES".to_string();
        }
    
        "NO".to_string()
    }