LeetCode Entry
1232. Check If It Is a Straight Line
Are all the x,y points in a line?
1232. Check If It Is a Straight Line easy blog post substack
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 \(tan_i = dy_i/dx_i = dy_0/dx_0\)
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
}
}