Check If It Is a Straight Line Problem
Description
LeetCode Problem 1232.
You are given an integer array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Example 1:

Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
2 <= coordinates.length <= 1000coordinates[i].length == 2-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4coordinatescontains no duplicate point.
Solution
Approach: Cross Product
The first two points (x1, y1) and (x2, y2) already fix the line, since the points are distinct. Every other point (x, y) lies on it exactly when the vector from the first point to it, (x - x1, y - y1), is parallel to the direction (dx, dy) = (x2 - x1, y2 - y1).
Comparing slopes (y - y1) / (x - x1) would mean dividing by zero on a vertical line and dealing with floating-point error. Two vectors are parallel exactly when their cross product is zero, so we test the same thing with integers only:
(x - x1) * dy == (y - y1) * dx
This also covers vertical (dx = 0) and horizontal (dy = 0) lines with no special case. With coordinates up to 10^4 in absolute value, each product is at most 4 * 10^8, so it fits in a 32-bit integer. If any point fails the test we return false; with only two points the answer is always true.
Complexity: O(n) time and O(1) extra space for n points.
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
x1, y1 = coordinates[0]
x2, y2 = coordinates[1]
dx, dy = x2 - x1, y2 - y1 # direction of the line through the first two points
for x, y in coordinates[2:]:
# (x - x1, y - y1) must be parallel to (dx, dy): their cross product is 0
if (x - x1) * dy != (y - y1) * dx:
return False
return True