3024. Type of Triangle #1701
-
Topics: You are given a 0-indexed integer array
Return a string representing the type of triangle that can be formed or Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the type of triangle that can be formed with three given side lengths. The possible types are equilateral, isosceles, scalene, or none if the sides cannot form a valid triangle. Approach
Let's implement this solution in PHP: 3024. Type of Triangle <?php
/**
* @param Integer[] $nums
* @return String
*/
function triangleType($nums) {
sort($nums);
$a = $nums[0];
$b = $nums[1];
$c = $nums[2];
if ($a + $b <= $c) {
return "none";
}
if ($a == $b && $b == $c) {
return "equilateral";
} elseif ($a == $b || $b == $c || $a == $c) {
return "isosceles";
} else {
return "scalene";
}
}
// Example usages:
echo triangleType([3, 3, 3]) . "\n"; // Output: equilateral
echo triangleType([3, 4, 5]) . "\n"; // Output: scalene
echo triangleType([5, 5, 8]) . "\n"; // Output: isosceles
echo triangleType([1, 2, 3]) . "\n"; // Output: none
?> Explanation:
This approach efficiently checks the validity and type of the triangle using sorting and simple comparisons, ensuring optimal performance for the given problem constraints. |
Beta Was this translation helpful? Give feedback.
We need to determine the type of triangle that can be formed with three given side lengths. The possible types are equilateral, isosceles, scalene, or none if the sides cannot form a valid triangle.
Approach