[LeetCode] Valid Boomerang

1037. Valid Boomerang

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
bool isBoomerang(vector<vector<int>>& points) {
sort(begin(points), end(points));
if(points[0][0] == points[1][0] and points[1][0] == points[2][0]) return false;
if(points[0][1] == points[1][1] and points[1][1] == points[2][1]) return false;
int da = points[1][0] - points[0][0], db = points[1][1] - points[0][1];
int dc = points[2][0] - points[1][0], dd = points[2][1] - points[1][1];
if(!da and !db) return false;
if(!dc and !dd) return false;
if(!da or !db or !dc or !dd) return true;
{
int g = __gcd(da,db);
da /= g, db /= g;
}
{
int g= __gcd(dc,dd);
dc /= g, dd /= g;
}
return da * dd != dc * db;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/10/PS/LeetCode/valid-boomerang/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.