[
geometry
math
]
BinarySearch 0138 Line Segment
Problem statement
https://binarysearch.com/problems/Line-Segment/
Solution
Just check some geometrical conditions: that all triples of points 0, 1, k
lies on the same line.
Complexity
It is O(n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, P):
if len(P) <= 2: return True
return all((P[0][0] - P[k][0])* (P[1][1] - P[k][1]) == (P[1][0] - P[k][0]) * (P[0][1] - P[k][1]) for k in range(2, len(P)))