1232. Check If It Is a Straight Line

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
int n = coordinates.size();
if(n==2) return true;
// int m = abs((coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0]) );
// avoid division by zero
int m_a = coordinates[1][1]-coordinates[0][1];
int m_b = coordinates[1][0]-coordinates[0][0];
for(int i=2;i<n;++i){
int temp_a = coordinates[i][1]-coordinates[i-1][1];
int temp_b = coordinates[i][0]-coordinates[i-1][0];

// if(m_a/m_b == temp_a/temp_b) return false;
if(m_a*temp_b != m_b*temp_a) return false;
}
return true;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)