Type of Triangle (LeetCode 3024): Solution and Explanation

Easy
Array
Math
Sorting

Given three side lengths, return "equilateral", "isosceles" or "scalene" for the triangle they form, or "none" if they cannot form one.

Intuition

Two questions in a fixed order: do the sides close into a triangle, and if so, how many of them match.

Picture laying the two shorter sides along the longest one. They can only lift up and meet at a corner if together they are longer than it. If they only just reach, the triangle is flat; if they fall short, they never meet. That is the triangle inequality, and once the sides are sorted it is the single check a + b > c.

Sorting also makes the equality checks easy. With a <= b <= c, a == c forces b to match as well, so all three are equal. Otherwise any equal pair must be neighbours in sorted order: a == b or b == c.

Approach: Sort, then check

  1. Sort the sides into a <= b <= c.
  2. If a + b <= c, return "none".
  3. If a == c, return "equilateral".
  4. If a == b or b == c, return "isosceles".
  5. Otherwise return "scalene".

Step-by-step example

nums = [3, 3, 3]
  1. Sorted: a = 3, b = 3, c = 3.
  2. a + b = 6 > 3, so a triangle forms.
  3. a == c, so all three sides are equal.
  4. Return "equilateral".

Code: Python, Java, C++

1class Solution:
2    def triangleType(self, nums: List[int]) -> str:
3        a, b, c = sorted(nums)
4        if a + b <= c:
5            return "none"
6        if a == c:
7            return "equilateral"
8        if a == b or b == c:
9            return "isosceles"
10        return "scalene"
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

Sorting three numbers and a few comparisons.

Space

O(1)

A copy of three integers at most.

Other approaches

Check all three inequalities (time O(1), space O(1))

Without sorting, test x + y > z, x + z > y and y + z > x, then count equal pairs. Same result with more comparisons.

Count distinct lengths (time O(1), space O(1))

After the triangle check, put the sides in a set. Size 1 means equilateral, 2 means isosceles, 3 means scalene.

Common mistakes

  • Classifying by equal sides before checking the triangle inequality. [1, 1, 2] has two equal sides but is flat, so the answer is "none".
  • Using a + b >= c. When a + b equals c the sides lie in a straight line, which is not a triangle.
  • Treating an equilateral triangle as isosceles because it also has two equal sides. Check the all-equal case first.

Frequently asked questions

Why is checking a + b > c enough after sorting?

The other two inequalities involve c on the left side, and c is the largest, so a + c > b and b + c > a always hold for positive lengths.

Why does a == c mean all three sides are equal?

b sits between a and c in sorted order. If the smallest and largest are equal, b has nowhere else to be.

Should I sort the input array in place?

It works, but sorting a copy keeps the caller's array unchanged, which is a good habit in interviews.