1037. Valid Boomerang #
Problem #
A boomerang is a set of 3 points that are all distinct and not in a straight line.
Given a list of three points in the plane, return whether these points are a boomerang.
Example 1:
Input: [[1,1],[2,3],[3,2]]
Output: true
Example 2:
Input: [[1,1],[2,2],[3,3]]
Output: false
Note:
points.length == 3points[i].length == 20 <= points[i][j] <= 100
Problem Summary #
A boomerang is defined as a set of three points that are all distinct and not in a straight line. Given a list of three points in the plane, determine whether these points can form a boomerang.
Solution Ideas #
- Determine whether the given 3 sets of points can satisfy the boomerang condition.
- Easy problem. Determine whether the slopes of the 2 lines formed by the 3 points are equal. Since calculating the slope involves division and may encounter a denominator of 0, it can be converted into multiplication: cross-multiply and then determine whether they are equal. This avoids having to handle the case where the denominator is 0, and the code is also simplified to one line.
Code #
package leetcode
func isBoomerang(points [][]int) bool {
return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1])
}