1394. Triangle Kind

Easy1.5 points estimatedGeometryMaths

A tile cutter is given three marks on a sheet of glass, corners[0], corners[1] and corners[2], each an integer point [x, y]. Before cutting, the worker wants to know what kind of triangle the marks form.

Return 0 if the three marks lie on one straight line (this includes marks that coincide). Otherwise return 1 if every angle is acute, 2 if one angle is exactly a right angle, and 3 if one angle is obtuse. Decide the angles exactly using integer dot products of the edge vectors at each mark: positive means acute, zero means right, negative means obtuse. Coordinates reach 10^4, so use wide enough integers. Aim for O(1) time and space.

Example 1

Input:
corners = [[0,0],[4,0],[0,3]]
Output:
2
Explanation:

The angle at the origin is exactly 90 degrees (dot product 0), so the answer is 2.

Example 2

Input:
corners = [[0,0],[2,0],[1,5]]
Output:
1
Explanation:

All three dot products are positive, so the triangle is acute.

Example 3

Input:
corners = [[0,0],[4,0],[1,1]]
Output:
3
Explanation:

At the point (1, 1) the dot product is negative, so the triangle is obtuse.

Constraints

corners.length == 3; each corner has two integers; -104 ≤ x, y ≤ 104.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…