#3024

Type of Triangle

Easy
ArrayMathSortingArrayMath
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(1)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution leverages the properties of sets to efficiently determine the number of unique side lengths and checks the triangle inequality in a single pass.

⚙️

Algorithm

3 steps
  1. 1Step 1: Check if the sides can form a triangle using the triangle inequality theorem.
  2. 2Step 2: Use a set to count the unique side lengths.
  3. 3Step 3: Return the triangle type based on the size of the set.
solution.py5 lines
1def triangleType(nums):
2    a, b, c = nums
3    if a + b > c and a + c > b and b + c > a:
4        return 'equilateral' if len(set(nums)) == 1 else 'isosceles' if len(set(nums)) == 2 else 'scalene'
5    return 'none'

Complexity note: The complexity remains constant as we only evaluate three sides.

  • 1Understanding the triangle inequality theorem is crucial for determining if three sides can form a triangle.
  • 2Counting unique side lengths helps classify the triangle efficiently.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.