-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
35 lines (27 loc) · 910 Bytes
/
Solution.cs
File metadata and controls
35 lines (27 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace LeetCode.Problem976{
//976. Largest Perimeter Triangle
//https://leetcode.com/problems/largest-perimeter-triangle/
/*
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths.
If it is impossible to form any triangle of a non-zero area, return 0.
*/
public class Solution {
public int LargestPerimeter(int[] nums) {
var sides = nums.OrderByDescending(p => p).ToList();
int first = sides[0];
int second = sides[1];
int third = sides[2];
if (first < second + third)
return first + second + third;
for (int i = 3; i < sides.Count; i++)
{
first = second;
second = third;
third = sides[i];
if (first < second + third)
return first + second + third;
}
return 0;
}
}
}