Leetcode: Valid Boomerang

Valid Boomerang



Similar Problems:


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:

  1. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// Blog link: https://code.dennyzhang.com/valid-boomerang
// Basic Idea: math
//    If not in the same line, these three points will be a triangle
//    Caculate the area of the triangle
// Complexity: Time O(1), Space O(1)
func isBoomerang(points [][]int) bool {
    return 2*(points[2][0]-points[0][0])*(points[1][1]-points[2][1]) != 
        (points[2][0]-points[0][0])*(points[0][1]-points[2][1]) + 
        (points[1][0]-points[0][0])*(points[1][1]-points[0][1]) + 
        (points[2][0]-points[1][0])*(points[1][1]-points[2][1])
}
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.