976. Largest Perimeter Triangle
Solution
class Solution {
public:
int largestPerimeter(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (int i = nums.size() - 3; i >= 0; i--) {
if (nums[i] + nums[i+1] > nums[i+2]) {
return nums[i] + nums[i+1] + nums[i+2];
}
}
return 0;
}
};
Complexity
- Time Complexity: O(NlogN), where N is the size of
nums
. - Space Complexity: O(1).