1037. Valid Boomerang
Description
Given an array points
where points[i] = [xi, yi]
represents a point on the X-Y plane, return true
if these points are a boomerang.
A boomerang is a set of three points that are all distinct and not in a straight line.
Example 1:
Input: points = [[1,1],[2,3],[3,2]] Output: true
Example 2:
Input: points = [[1,1],[2,2],[3,3]] Output: false
Constraints:
points.length == 3
points[i].length == 2
0 <= xi, yi <= 100
Solutions
Solution 1: Slope Comparison
Let the three points be \((x_1, y_1)\), \((x_2, y_2)\), and \((x_3, y_3)\). The formula for calculating the slope between two points is \(\frac{y_2 - y_1}{x_2 - x_1}\).
To ensure that the three points are not collinear, the condition \(\frac{y_2 - y_1}{x_2 - x_1} \neq \frac{y_3 - y_2}{x_3 - x_2}\) must be satisfied. By transforming the equation, we get \((y_2 - y_1) \cdot (x_3 - x_2) \neq (y_3 - y_2) \cdot (x_2 - x_1)\).
Note:
- When the slope between two points does not exist, i.e., \(x_1 = x_2\), the transformed equation still holds.
- If there are precision issues with division in slope comparison, it can be converted to multiplication.
Time complexity is \(O(1)\).
1 2 3 4 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|