1395. Same Side Tally
A surveyor stands at point a and sights toward point b, which fixes a directed line running from a to b and extending forever in both directions. Every stake in points lies to the left of the sight line, to the right of it, or exactly on it.
Both a and b are given as [x, y] and are different points. Return an array [left, on, right]: how many stakes are strictly to the left (positive cross product of b - a with p - a), how many lie exactly on the infinite line, and how many are strictly to the right. A stake equal to a or b counts as on the line, and duplicates count separately. Use 64-bit arithmetic for the cross products. Your solution should run in O(n) time and O(1) extra space.
Example 1
- Input:
- a = [0,0], b = [4,0], points = [[1,1],[2,-3],[7,0]]
- Output:
- [1,1,1]
- Explanation:
Looking along the x-axis, (1,1) is on the left, (2,-3) is on the right and (7,0) is on the line.
Example 2
- Input:
- a = [1,1]b = [2,3]points = [[1,1],[2,3],[3,5],[0,0],[5,2]]
- Output:
- [1,3,1]
- Explanation:
The line y = 2x - 1 holds (1,1), (2,3) and (3,5); (0,0) is on the left and (5,2) on the right.
Constraints
1 ≤ points.length ≤ 1000
-106 ≤ every coordinate ≤ 106
a and b are distinct points
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(1)