# 05.06.2023 [1232. Check If It Is a Straight Line]
05.06.2023
1232. Check If It Is a Straight Line medium
blog post
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/236
Problem TLDR
Are all the x,y
points in a line?
Intuition
We can compare tani=dyi/dxi=dy0/dx0t
Approach
corner case is a vertical line
Complexity
Time complexity:
O(n)Space complexity:
O(1)
Code
fun checkStraightLine(coordinates: Array<IntArray>): Boolean =
with((coordinates[1][1] - coordinates[0][1])/
(coordinates[1][0] - coordinates[0][0]).toDouble()) {
coordinates.drop(2).all {
val o = (it[1] - coordinates[0][1]) / (it[0] - coordinates[0][0]).toDouble()
isInfinite() && o.isInfinite() || this == o
}
}