Jump Game in Rust

impl Solution {
    pub fn can_jump(nums: Vec<i32>) -> bool {
        let mut max_reachable = 0; // The maximum index that can be reached

        for (i, &jump_length) in nums.iter().enumerate() {
            if i > max_reachable {
                return false; // You can't reach this index
            }
            // Update max_reachable, using usize for indexing
            max_reachable = std::cmp::max(max_reachable, i + jump_length as usize);

            // Check if the last index is reachable
            if max_reachable >= nums.len() - 1 {
                return true;
            }
        }

        // If the loop completes without returning true, it's not possible to reach the end
        false
    }
}

PrevNext