Teaching Kids Programming – Count the Intersection Points Given Intervals (Line Sweep Algorithm)


Teaching Kids Programming: Videos on Data Structures and Algorithms

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

Count the Intersection Points Given Intervals (Line Sweep Algorithm)

Given the size of the array is maximum 100, we can create 100 buckets, and then loop over each interval to fill each bucket (discrete point). Then we count how many buckets are filled.

1
2
3
4
5
6
7
class Solution:
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        data = [0] * 101
        for a, b in nums:
            for i in range(a, b + 1):
                data[i] = 1
        return sum(data)
class Solution:
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        data = [0] * 101
        for a, b in nums:
            for i in range(a, b + 1):
                data[i] = 1
        return sum(data)

Similar to counting the number of overlapped intervals, we can apply the Line Sweeping Algorithm. We mark the start plus one, and mark the end minus one. Then we track the balance from left to right. If the balance is above zero, we know the current point is taken.

1
2
3
4
5
6
7
8
9
10
11
12
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        data = [0] * 102
        for a, b in nums:
            data[a] += 1
            data[b + 1] -= 1
        x = 0
        ans = 0
        for i in range(1, 101):
            x += data[i]
            if x > 0:
                ans += 1
        return ans
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        data = [0] * 102
        for a, b in nums:
            data[a] += 1
            data[b + 1] -= 1
        x = 0
        ans = 0
        for i in range(1, 101):
            x += data[i]
            if x > 0:
                ans += 1
        return ans

Another solution is to sort the intervals by the start point. And then accumulate the points one by one by comparing the relation [a, b] to the previous endpoint p. There are 3 situations: a is larger than p, a is smaller than p but b is bigger than p, b is smaller than p. The time complexity is O(NLogN) due to sorting the N intervals.

1
2
3
4
5
6
7
8
9
10
11
12
13
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        if not nums:
            return 0
        nums.sort(key=lambda x: x[0])
        prev = -inf
        ans = 0
        for a, b in nums:
            if a > prev:
                ans += b - a + 1
            else:
                ans += max(0, b - prev)
            prev = max(prev, b)            
        return ans
    def numberOfPoints(self, nums: List[List[int]]) -> int:
        if not nums:
            return 0
        nums.sort(key=lambda x: x[0])
        prev = -inf
        ans = 0
        for a, b in nums:
            if a > prev:
                ans += b - a + 1
            else:
                ans += max(0, b - prev)
            prev = max(prev, b)            
        return ans

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
581 words
Last Post: Avoid Single Point of Failures by Introducing Multiple Master Backup Reader Node on Steem Bot Applications
Next Post: Teaching Kids Programming - Minimum Right Shifts to Sort the Array

The Permanent URL is: Teaching Kids Programming – Count the Intersection Points Given Intervals (Line Sweep Algorithm)

Leave a Reply