Skip to content
LC-0976 Easy LeetCode

976. Largest Perimeter Triangle

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 57% Topics: Array, Math, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

class Solution(object):
    def largestPerimeter(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        A.sort()
        for i in reversed(xrange(len(A) - 2)):
            if A[i] + A[i+1] > A[i+2]:
                return A[i] + A[i+1] + A[i+2]
        return 0

Solution from kamyu104/LeetCode-Solutions · MIT