Opening the reading…
Opening the reading…
PREFIX SUM › LINE SWEEP
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A point is covered when at least one car is active at that coordinate. Instead of visiting every point inside every interval, record where coverage starts and where it stops. For a car from s to e, coverage increases at s and must decrease immediately after e, at e + 1, because e itself is still covered.
A running sum of those changes tells you how many cars cover the current point. If the sum is positive, the point belongs to at least one car; if it is zero, no car reaches it. The coordinates are limited to 1 through 100, so one sweep over that fixed range is enough after all intervals have contributed their boundary changes.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n + 100) | Each of the n intervals contributes two constant-time boundary updates, and the final sweep examines exactly 100 coordinates once. No interval is revisited during the sweep, so the work is not multiplied by the number of covered points. |
| Space | O(100) extra | The difference array has 102 integer slots, including the position after coordinate 100. The returned count is output and is excluded from extra space. Because the coordinate range is fixed, this remains constant even in the worst case of 100 intervals with widely overlapping or disjoint endpoints. |
#include <vector>
using namespace std;
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
int diff[102] = {0};
for (auto& interval : nums) {
int start = interval[0];
int end = interval[1];
diff[start]++;
diff[end + 1]--;
}
int coveredCars = 0;
int answer = 0;
for (int point = 1; point <= 100; point++) {
coveredCars += diff[point];
if (coveredCars > 0) {
answer++;
}
}
return answer;
}
};The placement of the decrement is the central detail. For an interval [start, end], diff[end + 1]-- leaves the running count positive at end itself and removes the car before the next point is tested. The sweep updates coveredCars before checking it, so the changes at the current coordinate affect that coordinate rather than the following one.
You can also sort the intervals by start and merge every interval that overlaps the current merged interval. For each merged inclusive interval [left, right], add right - left + 1 to the answer. When the next interval starts after right, finish the current group and start a new one. This is a different arrangement, not an optimisation here: it costs O(n log n) time for sorting but does not depend on the maximum coordinate.
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
sort(nums.begin(), nums.end());
int answer = 0;
int left = nums[0][0];
int right = nums[0][1];
for (int i = 1; i < static_cast<int>(nums.size()); i++) {
int nextLeft = nums[i][0];
int nextRight = nums[i][1];
if (nextLeft > right + 1) {
answer += right - left + 1;
left = nextLeft;
right = nextRight;
} else {
right = max(right, nextRight);
}
}
answer += right - left + 1;
return answer;
}
};