2848. Points That Intersect With Cars LeetCode Solution
In this guide, you will get 2848. Points That Intersect With Cars LeetCode Solution with the best time and space complexity. The solution to Points That Intersect With Cars problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Points That Intersect With Cars solution in C++
- Points That Intersect With Cars solution in Java
- Points That Intersect With Cars solution in Python
- Additional Resources
Problem Statement of 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.
Example 1:
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:
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(100) = O(1)
2848. Points That Intersect With Cars LeetCode Solution in C++
class Solution {
public:
int numberOfPoints(vector<vector<int>>& nums) {
constexpr int kMax = 100;
int ans = 0;
int runningSum = 0;
vector<int> count(kMax + 2);
for (const vector<int>& num : nums) {
const int start = num[0];
const int end = num[1];
++count[start];
--count[end + 1];
}
for (int i = 1; i <= kMax; ++i) {
runningSum += count[i];
if (runningSum > 0)
++ans;
}
return ans;
}
};
/* code provided by PROGIEZ */
2848. Points That Intersect With Cars LeetCode Solution in Java
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
final int kMax = 100;
int ans = 0;
int runningSum = 0;
int[] count = new int[kMax + 2];
for (List<Integer> num : nums) {
final int start = num.get(0);
final int end = num.get(1);
++count[start];
--count[end + 1];
}
for (int i = 1; i <= kMax; i++) {
runningSum += count[i];
if (runningSum > 0)
++ans;
}
return ans;
}
}
// code provided by PROGIEZ
2848. Points That Intersect With Cars LeetCode Solution in Python
class Solution:
def numberOfPoints(self, nums: list[list[int]]) -> int:
kMax = 100
ans = 0
runningSum = 0
count = [0] * (kMax + 2)
for start, end in nums:
count[start] += 1
count[end + 1] -= 1
for i in range(1, kMax + 1):
runningSum += count[i]
if runningSum > 0:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.