This is a LeetCode solution. Can you write an equivalent solution that's better in any…
- User
This is a LeetCode solution. Can you write an equivalent solution that's better in any way? Faster, more memory efficient, cleaner, more idiomatic, etc...
pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> { let mut asteroids = &asteroids[..]; let mut acc = Vec::new(); loop { if asteroids.is_empty() { return acc; } match (acc.last(), asteroids[0]) { (None, _) | (Some(..0), _) | (_, 1..) => { acc.push(asteroids[0]); asteroids = &asteroids[1..]; } // Equal (Some(x), y) if *x == -y => { acc.pop(); asteroids = &asteroids[1..]; } // Right moving wins (Some(x), y) if -y < *x => { asteroids = &asteroids[1..]; } // Left moving wins (Some(_), _) => { acc.pop(); } } } } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.