Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 금융문자분석경진대회
- dacon
- Docker
- hackerrank
- leetcode
- 백준
- AI 경진대회
- 편스토랑 우승상품
- Git
- ubuntu
- 우분투
- 파이썬
- 더현대서울 맛집
- 자연어처리
- Baekjoon
- ChatGPT
- 편스토랑
- github
- 캐치카페
- 프로그래머스
- Real or Not? NLP with Disaster Tweets
- 프로그래머스 파이썬
- programmers
- PYTHON
- 코로나19
- SW Expert Academy
- 데이콘
- 맥북
- gs25
- Kaggle
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 1232. Check If It Is a Straight Line (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 1232. Check If It Is a Straight Line (Python)
솜씨좋은장씨 2021. 2. 1. 20:46728x90
반응형
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.
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
- 2 <= coordinates.length <= 1000
- coordinates[i].length == 2
- -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
- coordinates contains no duplicate point.
Solution
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
x_points = [x[0] for x in coordinates]
if len(set(x_points)) == 1:
return True
else:
point1_x, point1_y = coordinates[0]
point2_x, point2_y = coordinates[1]
dy = (point1_y-point2_y)
dx = (point1_x-point2_x)
if dx == 0:
dx = 1
bias = point1_y - dy/dx * point1_x
for i in range(2,len(coordinates)):
point_x, point_y = coordinates[i]
if point_y != dy/dx* point_x + bias:
return False
return True
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 389. Find the Difference (Python) (0) | 2021.02.03 |
---|---|
[leetCode] 162. Find Peak Element (Python) (0) | 2021.02.02 |
[leetCode] 1009. Complement of Base 10 Integer (Python) (0) | 2021.01.31 |
[leetCode] 328. Odd Even Linked List (Python) (0) | 2021.01.30 |
[leetCode] 941. Valid Mountain Array (Python) (0) | 2021.01.29 |
Comments