Problem

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 withany part of a car.

Examples

Example 1

1
2
3
Input: nums = [[3,6],[1,5],[4,7]]
Output: 7
Explanation: All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.

Example 2

1
2
3
Input: nums = [[1,3],[5,8]]
Output: 7
Explanation: Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7.

Constraints

  • 1 <= nums.length <= 100
  • nums[i].length == 2
  • 1 <= starti <= endi <= 100

Solution

Intuition

We need to count all unique integer points that are covered by at least one car’s interval. Since the intervals may overlap, we must avoid double-counting points.

Approach

We can use a set to collect all integer points covered by any car. For each interval [start, end], add all integers from start to end (inclusive) to the set. The answer is the size of the set.

Alternatively, we can use a boolean array of size 101 (since endi ≤ 100) to mark covered points, then count the number of True entries.

Code

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <vector>
#include <unordered_set>
using namespace std;

class Solution {
public:
    int numberOfPoints(vector<vector<int>>& nums) {
        unordered_set<int> covered;
        for (const auto& car : nums) {
            for (int i = car[0]; i <= car[1]; ++i) {
                covered.insert(i);
            }
        }
        return covered.size();
    }
};
Go
1
2
3
4
5
6
7
8
9
func numberOfPoints(nums [][]int) int {
    covered := make(map[int]bool)
    for _, car := range nums {
        for i := car[0]; i <= car[1]; i++ {
            covered[i] = true
        }
    }
    return len(covered)
}
Java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import java.util.*;
class Solution {
    public int numberOfPoints(int[][] nums) {
        Set<Integer> covered = new HashSet<>();
        for (int[] car : nums) {
            for (int i = car[0]; i <= car[1]; i++) {
                covered.add(i);
            }
        }
        return covered.size();
    }
}
Kotlin
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun numberOfPoints(nums: Array<IntArray>): Int {
        val covered = mutableSetOf<Int>()
        for (car in nums) {
            for (i in car[0]..car[1]) {
                covered.add(i)
            }
        }
        return covered.size
    }
}
Python
1
2
3
4
5
6
class Solution:
    def numberOfPoints(self, nums: list[list[int]]) -> int:
        covered = set()
        for start, end in nums:
            covered.update(range(start, end + 1))
        return len(covered)
Rust
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use std::collections::HashSet;
impl Solution {
    pub fn number_of_points(nums: Vec<Vec<i32>>) -> i32 {
        let mut covered = HashSet::new();
        for car in nums.iter() {
            for i in car[0]..=car[1] {
                covered.insert(i);
            }
        }
        covered.len() as i32
    }
}
TypeScript
1
2
3
4
5
6
7
8
9
function numberOfPoints(nums: number[][]): number {
    const covered = new Set<number>();
    for (const [start, end] of nums) {
        for (let i = start; i <= end; i++) {
            covered.add(i);
        }
    }
    return covered.size;
}

Complexity

  • ⏰ Time complexity: O(N * K), where N is the number of cars and K is the average length of each interval (at most 100).
  • 🧺 Space complexity: O(1) (since the number of possible points is at most 100).