Problem Solving/LeetCode
2023.06.05 Today's Challenge
fabichoi
2023. 6. 5. 23:45
https://leetcode.com/problems/check-if-it-is-a-straight-line/
Check If It Is a Straight Line - LeetCode
Can you solve this real interview question? Check If It Is a Straight Line - 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.
leetcode.com
오랫만에 직접 풀기 시도했는데, divided by zero 때문에 실패 계속 뜸 ㅠㅠ
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
coordinates.sort(key=lambda x: (x[0], x[1]))
fir, sec = coordinates[0], coordinates[1]
for i in range(2, len(coordinates)):
c = coordinates[i]
if (c[0] - fir[0]) * (sec[1] - fir[1]) != (c[1] - fir[1]) * (sec[0] - fir[0]):
return False
return True
반응형