← Home

1232. Check If It Is a Straight Line

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
123

Let's make this one less mysterious. For 1232. Check If It Is a Straight Line, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are checkStraightLine.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 8 ms, faster than 91.80% of C++ online submissions for Check If It Is a Straight Line.
02//Memory Usage: 10.5 MB, less than 100.00% of C++ online submissions for Check If It Is a Straight Line.
03class Solution {
04public:
05    bool checkStraightLine(vector<vector<int>>& coordinates) {
06        //remember to convert to "double"!
07        double slope = (double)(coordinates[1][1] - coordinates[0][1]) / 
08                (coordinates[1][0] - coordinates[0][0]);
09        // cout << slope << endl;
10        for(int i = 2; i < coordinates.size(); i++){
11            vector<int> a = coordinates[i], b = coordinates[i-1];
12            // cout << (double)(a[1] - b[1]) / (a[0] - b[0]) << endl;
13            if(slope != (double)(a[1] - b[1]) / (a[0] - b[0])){
14                return false;
15            }
16        }
17        cout << endl;
18        
19        return true;
20    }
21};
22
23//cross prodcut
24//Runtime: 20 ms, faster than 14.27% of C++ online submissions for Check If It Is a Straight Line.
25//Memory Usage: 10.2 MB, less than 100.00% of C++ online submissions for Check If It Is a Straight Line.
26class Solution {
27public:
28    bool checkStraightLine(vector<vector<int>>& coordinates) {
29        int n = coordinates.size();
30        
31        if(n == 2) return true;
32        
33        //the vector from coordinates[0] to coordinates[1]
34        vector<int> v1 = {coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1]};
35        vector<int> v2;
36        
37        for(int i = 2; i < n; i++){
38            v2 = {coordinates[i][0] - coordinates[0][0], coordinates[i][1] - coordinates[0][1]};
39            //cross product
40            //this is equivalent to comparing their slope: v2[1]/v2[0] == v1[1]/v1[0]
41            if(v1[0]*v2[1] - v1[1]*v2[0] != 0) return false;
42        }
43        
44        return true;
45    }
46};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.