Points That Intersect With Cars - Problem

You are given a 0-indexed 2D integer array nums representing the coordinates of 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.

Input & Output

Example 1 — Basic Overlapping Cars
$ Input: nums = [[3,6],[1,5],[4,7]]
Output: 7
💡 Note: Cars cover points: [3,6] covers 3,4,5,6; [1,5] covers 1,2,3,4,5; [4,7] covers 4,5,6,7. Combined unique points: 1,2,3,4,5,6,7 = 7 total points.
Example 2 — Non-overlapping Cars
$ Input: nums = [[1,3],[5,8]]
Output: 7
💡 Note: Car [1,3] covers points 1,2,3 (3 points). Car [5,8] covers points 5,6,7,8 (4 points). No overlap, so total is 3 + 4 = 7 points.
Example 3 — Single Car
$ Input: nums = [[2,5]]
Output: 4
💡 Note: Only one car covering points 2,3,4,5, which gives us 4 integer points total.

Constraints

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

Visualization

Tap to expand
Points That Intersect With CarsInput: nums = [[3,6],[1,5],[4,7]]Number Line12345678Car 1: [1,5]Car 2: [3,6]Car 3: [4,7]Covered Points: 1,2,3,4,5,6,7Output: 7 unique points
Understanding the Visualization
1
Input
Cars positioned as intervals on number line
2
Coverage
Each car covers all integer points in its range
3
Count
Total unique integer points covered
Key Takeaway
🎯 Key Insight: Each car covers all integer points from start to end inclusive, use set to handle overlaps automatically
Asked in
Google 12 Amazon 8 Microsoft 6
12.5K Views
Medium Frequency
~15 min Avg. Time
384 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen