3024. Type of Triangle
EasyView on LeetCode
Time: O(1)
Space: O(1)
Problem Overview
Given three stick lengths, they form a non-zero area triangle iff the sum of the two smaller sides exceeds the largest.
Intuition
Given three stick lengths, they form a non-zero area triangle iff the sum of the two smaller sides exceeds the largest. Sort three numbers and check nums[0]+nums[1] > nums[2].
Algorithm
- 1Sort the three sides ascending.
- 2If nums[0] + nums[1] > nums[2], return the triangle type required (e.g. "yes" / specific label).
- 3Else degenerate or impossible triangle.
Example Walkthrough
Input: nums = [2,3,4]
- 1.Sorted: 2,3,4. 2+3=5 > 4 — valid triangle.
Output: valid triangle classification
Common Pitfalls
- •After sorting, only one inequality check needed.
- •Equality (2+3=5) is degenerate — not a valid triangle for positive area.
- •Exactly three sides in problem.
3024.cs
C#
// Approach: Sort sides; check triangle validity then classify equilateral/isosceles/scalene.
// Time: O(1) Space: O(1)
public class Solution
{
public string TriangleType(int[] nums)
{
Array.Sort(nums);
if (nums[0] + nums[1] <= nums[2])
return "none";
if (nums[0] == nums[1] && nums[1] == nums[2])
return "equilateral";
if (nums[0] == nums[1] || nums[1] == nums[2])
return "isosceles";
return "scalene";
}
}Was this solution helpful?
Related Problems
- 179. Largest Number(Medium)
- 368. Largest Divisible Subset(Medium)
- 539. Minimum Time Difference(Medium)
- 857. Minimum Cost to Hire K Workers(Hard)
- 869. Reordered Power of 2(Medium)
- 973. K Closest Points to Origin(Medium)