[
math
geometry
]
Leetcode 1232. Check If It Is a Straight Line
Problem statement
https://leetcode.com/problems/check-if-it-is-a-straight-line/
Solution
Just check that all triples 0, 1, k
lies on the same line.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def checkStraightLine(self, C):
n = len(C)
dx = C[1][0] - C[0][0]
dy = C[1][1] - C[0][1]
for i in range(2, n):
dx2 = C[i][0] - C[0][0]
dy2 = C[i][1] - C[0][1]
if dx*dy2 != dy*dx2: return False
return True