Problem Description
Given a 0-indexed integer array nums of size 3 representing the sides of a potential triangle, determine the type of triangle that can be formed. Return "equilateral" if all sides are equal, "isosceles" if exactly two sides are equal, "scalene" if all sides are different, and "none" if the triangle conditions are not met.
Key Insights
- The triangle inequality theorem must be satisfied: the sum of any two sides should be greater than the third.
- Check if all sides are equal for an equilateral triangle.
- For an isosceles triangle, exactly two sides must match.
- For a scalene triangle, all three sides must be of different lengths.
- Sorting the array can simplify the triangle inequality check.
Space and Time Complexity
Time Complexity: O(1) – as the input size is fixed with 3 elements. Space Complexity: O(1) – only a constant amount of extra space is used.
Solution
The solution uses a simple conditional checking strategy. First, verify the triangle inequality by ensuring that the sum of the two smallest sides is greater than the largest side (can be efficiently done by sorting the array). Once the triangle validity is confirmed, determine the triangle type by comparing the sides:
- Use equality checks to determine if all three sides are equal (equilateral).
- Then, check if exactly two sides are equal (isosceles).
- If neither of those conditions is met, then the triangle must be scalene. Data structures used include arrays and the algorithmic approach is to use sorting (for convenience) and simple conditional checks.