Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 918 Bytes

1232_check_if_it_is_a_straight_line.md

File metadata and controls

31 lines (24 loc) · 918 Bytes

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.

Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false

Solution

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        (x1, y1) = coordinates[0]
        (x2, y2) = coordinates[1]

        for (x3, y3) in coordinates[2:]:
            if (y2 - y1) * (x3 - x1) != (y3 - y1) * (x2 - x1):
                return False
        return True