Valid Boomerang - Math - Easy - LeetCode
💻 coding

Valid Boomerang - Math - Easy - LeetCode

1 min read 149 words
1 min read
ShareWhatsAppPost on X
  • 1A boomerang consists of three distinct points that do not lie on a straight line.
  • 2The algorithm checks if the area formed by the three points is non-zero to determine if they are a boomerang.
  • 3The time and space complexity of the solution is O(1), indicating constant time and space usage.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"A boomerang consists of three distinct points that do not lie on a straight line."

Valid Boomerang - Math - Easy - LeetCode

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 == 3 points[i].length == 2 0 <= points[i][j] <= 100

public class Solution {
 public bool IsBoomerang(int[][] points) {
 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]);
 }
}

Time Complexity: O(1)

Space Complexity: O(1)

Assuming three points are A, B, C.

The first idea is that, calculate the area of ABC.

The other idea is to calculate the slope of AB and AC. K_AB = (p[0][0] - p[1][0]) / (p[0][1] - p[1][1]) K_AC = (p[0][0] - p[2][0]) / (p[0][1] - p[2][1])

We check if K_AB != K_AC, instead of calculating a fraction.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 1 October 2020 · 1 min read · 149 words

Part of AskGif Blog · coding

You might also like