Classic problem #3024EasyArrayMathSortingPolygons
Type of Triangle
Real-world version: Naming the Shape of Three RodsTracked in this browser only
Read Type of Triangle on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
Three rod lengths are given as rods.
Return the name of the triangle they form:
"none"when they cannot form a triangle at all, which happens exactly when some two of them added together do not exceed the third;"equilateral"when all three are the same length;"isosceles"when exactly two are the same length;"scalene"when no two are the same length.
Examples
Example 1
- Input
- rods = [2, 2, 3]
- Output
- "isosceles"
Two and two added together exceed three, so a triangle is possible, and exactly two of the rods are the same length.
Example 2
- Input
- rods = [1, 1, 5]
- Output
- "none"
The two short rods add to two, which does not exceed five, so they cannot close into a triangle at all.
Example 3
- Input
- rods = [6, 7, 8]
- Output
- "scalene"
Six and seven exceed eight, so a triangle is possible, and no two rods are the same length.
Constraints
- rods.length == 3
- 1 <= rods[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def triangle_type(rods: list[int]) -> str:Java
public String triangleType(int[] rods)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's