파비의 매일매일 공부기록

2023.06.05 Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.06.07 Today's Challenge  (0) 2023.06.07
2023.06.06 Today's Challenge  (0) 2023.06.06
2023.06.04 Today's Challenge  (0) 2023.06.04
2023.06.03 Today's Challenge  (0) 2023.06.03
2023.06.02 Today's Challenge  (0) 2023.06.02
Comments