Skip to content

Commit ec30bab

Browse files
added 3024 lc solution
1 parent dffdd80 commit ec30bab

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
id: type-of-triangle
3+
title: Type of Triangle (LeetCode)
4+
sidebar_label: 3024-TypeOfTriangle
5+
tags:
6+
- Triangle
7+
- Array
8+
description: Determine the type of triangle that can be formed by an array of integers representing the sides of the triangle.
9+
sidebar_position: 3024
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :---------------- | :------------ | :--------------- |
16+
| [Type of Triangle](https://leetcode.com/problems/type-of-triangle/) | [Type of Triangle Solution on LeetCode](https://leetcode.com/problems/type-of-triangle/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) |
17+
18+
## Problem Description
19+
20+
You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.
21+
22+
A triangle is called equilateral if it has all sides of equal length.
23+
A triangle is called isosceles if it has exactly two sides of equal length.
24+
A triangle is called scalene if all its sides are of different lengths.
25+
Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.
26+
27+
### Example 1
28+
29+
- **Input:** `nums = [3,3,3]`
30+
- **Output:** `"equilateral"`
31+
- **Explanation:** Since all the sides are of equal length, therefore, it will form an equilateral triangle.
32+
33+
### Example 2
34+
35+
- **Input:** `nums = [3,4,5]`
36+
- **Output:** `"scalene"`
37+
- **Explanation:**
38+
- `nums[0] + nums[1] = 3 + 4 = 7`, which is greater than `nums[2] = 5`.
39+
- `nums[0] + nums[2] = 3 + 5 = 8`, which is greater than `nums[1] = 4`.
40+
- `nums[1] + nums[2] = 4 + 5 = 9`, which is greater than `nums[0] = 3`.
41+
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
42+
As all the sides are of different lengths, it will form a scalene triangle.
43+
44+
### Constraints
45+
46+
- `nums.length == 3`
47+
- `1 <= nums[i] <= 100`
48+
49+
## Approach
50+
51+
To determine the type of triangle that can be formed by the given sides, we need to:
52+
53+
1. Check if the given sides can form a valid triangle using the triangle inequality theorem.
54+
2. If the sides form a valid triangle, check if the triangle is equilateral, isosceles, or scalene based on the lengths of the sides.
55+
56+
### Solution Code
57+
58+
#### Python
59+
60+
```python
61+
class Solution:
62+
def triangleType(self, nums: List[int]) -> str:
63+
a, b, c = sorted(nums)
64+
65+
if a + b <= c:
66+
return "none"
67+
if a == b == c:
68+
return "equilateral"
69+
if a == b or b == c or a == c:
70+
return "isosceles"
71+
72+
return "scalene"
73+
```
74+
75+
#### C++
76+
```c++
77+
#include <vector>
78+
#include <string>
79+
#include <algorithm>
80+
using namespace std;
81+
82+
class Solution {
83+
public:
84+
string triangleType(vector<int>& nums) {
85+
sort(nums.begin(), nums.end());
86+
int a = nums[0], b = nums[1], c = nums[2];
87+
88+
if (a + b <= c) {
89+
return "none";
90+
}
91+
if (a == b && b == c) {
92+
return "equilateral";
93+
}
94+
if (a == b || b == c || a == c) {
95+
return "isosceles";
96+
}
97+
98+
return "scalene";
99+
}
100+
};
101+
102+
```
103+
104+
#### Java
105+
```java
106+
import java.util.Arrays;
107+
108+
class Solution {
109+
public String triangleType(int[] nums) {
110+
Arrays.sort(nums);
111+
int a = nums[0], b = nums[1], c = nums[2];
112+
113+
if (a + b <= c) {
114+
return "none";
115+
}
116+
if (a == b && b == c) {
117+
return "equilateral";
118+
}
119+
if (a == b || b == c || a == c) {
120+
return "isosceles";
121+
}
122+
123+
return "scalene";
124+
}
125+
}
126+
127+
```
128+
129+
### Conclusion
130+
The solutions use a simple sorting approach to determine the type of triangle that can be formed by
131+
the given sides. This ensures that the problem is solved in a straightforward and efficient manner
132+
across different programming languages.

0 commit comments

Comments
 (0)