Advertisement

Check If It Is a Straight Line - LeetCode 1232 Solution

Check If It Is a Straight Line - Complete Solution Guide

Check If It Is a Straight Line is LeetCode problem 1232, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an 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 <= 1000 coordinates[i].length == 2 -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4 coordinates contains no duplicate

Detailed Explanation

The task is to determine if a given set of `coordinates` points all lie on a single straight line within the XY plane. Each `coordinates[i]` is provided as an `[x, y]` pair. Essentially, we're asked to verify if every point is collinear with all others. For instance, if you're given `[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]`, the function should return `true` because these points form a clear diagonal line where `y = x + 1`. However, for `[[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]`, the output should be `false` because `[3,4]` deviates from the `y=x` pattern established by the first two points.

Solution Approach

The provided solution elegantly navigates the common pitfalls of direct slope calculation (like floating-point inaccuracies or division by zero for vertical lines) by reframing the collinearity check. It establishes a reference line using the very first two points, `(x0, y0)` and `(x1, y1)`. For every subsequent point `(x, y)` in the `coordinates` array, it checks if it aligns with this established line. The core insight lies in transforming the slope equality `(y - y0) / (x - x0) == (y1 - y0) / (x1 - x0)` into a cross-multiplication form: `(x1 - x0) * (y - y0) == (x - x0) * (y1 - y0)`. This effectively checks if the 'cross-product' of the vector from `(x0, y0)` to `(x1, y1)` and the vector from `(x0, y0)` to `(x, y)` is zero. If this equality holds, the points are collinear; if the products are unequal (`!=`), the point `(x, y)` does not lie on the line, and the function immediately returns `false`. This approach uses only integer arithmetic, ensuring precision and robustly handling all line orientations, including horizontal and vertical ones.

Step-by-Step Algorithm

  1. Step 1: Handle the base case: If the number of points is less than or equal to 2, return `true`.
  2. Step 2: Select two reference points (e.g., the first two points in the array).
  3. Step 3: Iterate through the remaining points.
  4. Step 4: For each point, calculate the cross product using the formula: (x1 - x0)(y - y0) - (x - x0)(y1 - y0), where (x0, y0) and (x1, y1) are the reference points and (x, y) is the current point. If this cross product is non-zero, the points are not collinear, and the function returns `false`.
  5. Step 5: If the loop completes without finding any non-collinear points, return `true`.

Key Insights

  • **Integer Cross-Product for Collinearity:** Instead of calculating and comparing floating-point slopes, which introduces precision issues and division-by-zero problems (especially for vertical lines), the solution leverages the fact that three points `(x0, y0)`, `(x1, y1)`, and `(x, y)` are collinear if and only if `(x1 - x0) * (y - y0) == (x - x0) * (y1 - y0)`. This algebraic manipulation of `slope_01 == slope_0i` allows for a robust, purely integer-based check.
  • **Reference Line from First Two Points:** Any two distinct points define a unique straight line. The solution wisely uses the first two points in the `coordinates` array (`coordinates[0]` and `coordinates[1]`) to establish this foundational line. All subsequent points are then checked against this *single, fixed* line, simplifying the logic significantly compared to comparing every possible pair of points.
  • **Early Exit for Efficiency:** The algorithm incorporates an immediate `return False` as soon as it encounters a point that violates the collinearity condition. If even one point is off the line, the entire set cannot form a straight line, so there's no need to continue processing the remaining points. This short-circuiting behavior provides an efficient optimization, especially for inputs where the points quickly diverge from a straight line.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Math, Geometry.

Companies

Asked at: Datadog, Palantir Technologies.