[LeetCode] Points That Intersect With Cars

2848. Points That Intersect With Cars

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
vector<int> cov(111);
for(auto n : nums) {
cov[n[0]] += 1;
cov[n[1] + 1] -= 1;
}
int res = 0;
for(int i = 1; i < cov.size(); i++) {
cov[i] += cov[i-1];
if(cov[i] > 0) res += 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/09/10/PS/LeetCode/points-that-intersect-with-cars/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.