Type of Triangle (LeetCode 3024): Solution and Explanation
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
- Sort the sides into
a <= b <= c. - If
a + b <= c, return"none". - If
a == c, return"equilateral". - If
a == borb == c, return"isosceles". - Otherwise return
"scalene".
Step-by-step example
nums = [3, 3, 3]
- Sorted: a = 3, b = 3, c = 3.
- a + b = 6 > 3, so a triangle forms.
- a == c, so all three sides are equal.
- 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"1class Solution {
2 public String triangleType(int[] nums) {
3 int[] s = nums.clone();
4 Arrays.sort(s);
5 if (s[0] + s[1] <= s[2]) {
6 return "none";
7 }
8 if (s[0] == s[2]) {
9 return "equilateral";
10 }
11 if (s[0] == s[1] || s[1] == s[2]) {
12 return "isosceles";
13 }
14 return "scalene";
15 }
16}1class Solution {
2public:
3 string triangleType(vector<int>& nums) {
4 vector<int> s(nums.begin(), nums.end());
5 sort(s.begin(), s.end());
6 if (s[0] + s[1] <= s[2]) {
7 return "none";
8 }
9 if (s[0] == s[2]) {
10 return "equilateral";
11 }
12 if (s[0] == s[1] || s[1] == s[2]) {
13 return "isosceles";
14 }
15 return "scalene";
16 }
17};Time and space complexity
O(1)
Sorting three numbers and a few comparisons.
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.