73 lines
1.4 KiB
Markdown
73 lines
1.4 KiB
Markdown
|
# Leetcode Check-If-It-Is-a-Straight-Line
|
|||
|
|
|||
|
#### 2022-07-27 11:38
|
|||
|
|
|||
|
> ##### Algorithms:
|
|||
|
> #algorithm #math
|
|||
|
> ##### Difficulty:
|
|||
|
> #coding_problem #difficulty-easy
|
|||
|
> ##### Additional tags:
|
|||
|
> #leetcode
|
|||
|
> ##### Revisions:
|
|||
|
> N/A
|
|||
|
|
|||
|
##### Related topics:
|
|||
|
##### Links:
|
|||
|
- [Link to problem](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
|
|||
|
___
|
|||
|
### Problem
|
|||
|
|
|||
|
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.
|
|||
|
|
|||
|
#### Examples
|
|||
|
|
|||
|
**Example 1:**
|
|||
|
|
|||
|
![](https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg)
|
|||
|
|
|||
|
```
|
|||
|
**Input:** coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
|
|||
|
**Output:** true
|
|||
|
```
|
|||
|
|
|||
|
**Example 2:**
|
|||
|
|
|||
|
**![](https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg)**
|
|||
|
|
|||
|
```
|
|||
|
**Input:** coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
|
|||
|
**Output:** false
|
|||
|
```
|
|||
|
|
|||
|
#### Constraints
|
|||
|
|
|||
|
### Thoughts
|
|||
|
|
|||
|
> [!summary]
|
|||
|
> This is a #math problem.
|
|||
|
|
|||
|
Many math problems can be simplified by:
|
|||
|
|
|||
|
- work out the expression
|
|||
|
- Simplify it.
|
|||
|
|
|||
|
This is also true in this problem.
|
|||
|
|
|||
|
We want to calculate the k for every consecutive points, if they are the same, they are on one straight line.
|
|||
|
|
|||
|
By starting from the first, using them as the base k:
|
|||
|
```
|
|||
|
k = (dx0 - dx1) / (dy0 - dy1) = (dx - dx') / (dy - dy')
|
|||
|
|
|
|||
|
v
|
|||
|
dx*yi - dy*xi = dx*y0 - dy*x0
|
|||
|
```
|
|||
|
|
|||
|
Which is the cross product of these two points.
|
|||
|
|
|||
|
### Solution
|
|||
|
|
|||
|
==#TODO: Fill this out==
|
|||
|
```cpp
|
|||
|
WIP
|
|||
|
```
|