-
Notifications
You must be signed in to change notification settings - Fork 0
/
976.largest-perimeter-triangle.cpp
69 lines (62 loc) · 1.33 KB
/
976.largest-perimeter-triangle.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode id=976 lang=cpp
*
* [976] Largest Perimeter Triangle
*
* https://leetcode.com/problems/largest-perimeter-triangle/description/
*
* algorithms
* Easy (54.23%)
* Likes: 1776
* Dislikes: 245
* Total Accepted: 131.9K
* Total Submissions: 246K
* Testcase Example: '[2,1,2]'
*
* 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.
*
*
* Example 1:
*
*
* Input: nums = [2,1,2]
* Output: 5
*
*
* Example 2:
*
*
* Input: nums = [1,2,1]
* Output: 0
*
*
*
* Constraints:
*
*
* 3 <= nums.length <= 10^4
* 1 <= nums[i] <= 10^6
*
*
*/
// @lc code=start
class Solution {
public:
int largestPerimeter(vector<int>& nums) {
sort(nums.begin(),nums.end());
int i= nums.size()-3;
//cout<<nums[i]+nums[i+1]+nums[i+2]<<endl;
for(int i= nums.size()-3;i>=0;i--){
if(nums[i]+nums[i+1]>nums[i+2]){
cout<<nums[i]<<endl;
cout<<nums[i+1]<<endl;
cout<<nums[i+2]<<endl;
return nums[i]+nums[i+1]+nums[i+2];
}
}
return 0;
}
};
// @lc code=end