Difficulty: Easy | Acceptance: 44.10% | Paid: No Topics: Array, Math, Sorting
You are given a 0-indexed array of positive integers nums. Find the type of triangle that can be formed with the side lengths nums[0], nums[1], and nums[2].
Return:
“equilateral” if it is an equilateral triangle. “isosceles” if it is an isosceles triangle. “scalene” if it is a scalene triangle. “none” if the three sides cannot form a triangle.
- Examples
- Constraints
- Approach 1: Direct Comparison
- Approach 2: Sorting
Examples
Example 1
Input:
nums = [3,3,3]
Output:
"equilateral"
Explanation: Since all the sides are of equal length, therefore, it will form an equilateral triangle.
Example 2
Input:
nums = [3,4,5]
Output:
"scalene"
Explanation: nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5. nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4. nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3. Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle. As all the sides are of different lengths, it will form a scalene triangle.
Constraints
3 <= nums.length <= 10
1 <= nums[i] <= 10
Approach 1: Direct Comparison
Intuition We can directly check the conditions for a valid triangle and then determine the type based on the equality of the sides. For a valid triangle, the sum of any two sides must be strictly greater than the third side.
Steps
- Extract the first three elements as
a,b, andc. - Check the triangle inequality theorem:
a + b > c,a + c > b, andb + c > a. If any of these fail, return “none”. - If
a == b == c, return “equilateral”. - If any two sides are equal (
a == borb == cora == c), return “isosceles”. - Otherwise, return “scalene”.
class Solution:
def triangleType(self, nums: list[int]) -> str:
a, b, c = nums[0], nums[1], nums[2]
if a + b <= c or a + c <= b or b + c <= a:
return "none"
if a == b == c:
return "equilateral"
if a == b or b == c or a == c:
return "isosceles"
return "scalene"Complexity
- Time: O(1) - We perform a constant number of operations on 3 elements.
- Space: O(1) - No extra space is used proportional to input size.
- Notes: This is the most efficient approach as it avoids the overhead of sorting.
Approach 2: Sorting
Intuition
By sorting the three side lengths, we simplify the triangle inequality check. If the sides are sorted as x <= y <= z, the triangle is valid if and only if x + y > z. This is because x + z > y and y + z > x are always true for positive numbers.
Steps
- Extract the first three elements into a new list/array.
- Sort the list in ascending order.
- Check if
sides[0] + sides[1] <= sides[2]. If true, return “none”. - Check for equilateral: if
sides[0] == sides[2], all sides are equal. - Check for isosceles: if
sides[0] == sides[1]orsides[1] == sides[2]. - Otherwise, return “scalene”.
class Solution:
def triangleType(self, nums: list[int]) -> str:
sides = sorted(nums[:3])
if sides[0] + sides[1] <= sides[2]:
return "none"
if sides[0] == sides[2]:
return "equilateral"
if sides[0] == sides[1] or sides[1] == sides[2]:
return "isosceles"
return "scalene"Complexity
- Time: O(1) - Sorting 3 elements is constant time.
- Space: O(1) - We store at most 3 elements.
- Notes: While still O(1), this approach has slightly higher constant factors due to sorting, but offers cleaner logic for the triangle inequality check.