Skip to main content
Back to problems
#2848
Easy Algorithms

Points that intersect with cars

Array Hash Table Prefix Sum
73.4% acceptance
Feb 25, 2026
370
35
You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covered with any part of a car.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn number_of_points(nums: Vec<Vec<i32>>) -> i32 {
    let mut covered = [false; 102];
    for seg in &nums { for p in seg[0]..=seg[1] { covered[p as usize] = true; } }
    covered.iter().filter(|&&v| v).count() as i32
  }
}