LeetCode Solutions
976. Largest Perimeter Triangle
Time: Space:
class Solution {
public:
int largestPerimeter(vector<int>& A) {
sort(begin(A), end(A));
for (int i = A.size() - 1; i > 1; --i)
if (A[i - 2] + A[i - 1] > A[i])
return A[i - 2] + A[i - 1] + A[i];
return 0;
}
};
class Solution {
public int largestPerimeter(int[] A) {
Arrays.sort(A);
for (int i = A.length - 1; i > 1; --i)
if (A[i - 2] + A[i - 1] > A[i])
return A[i - 2] + A[i - 1] + A[i];
return 0;
}
}
class Solution:
def largestPerimeter(self, A: List[int]) -> int:
A = sorted(A)
for i in range(len(A) - 1, 1, -1):
if A[i - 2] + A[i - 1] > A[i]:
return A[i - 2] + A[i - 1] + A[i]
return 0