↓Skip to main content
  1. LeetCode/

LeetCode 976: Largest Perimeter Triangle

·2 mins· ·
LeetCode Easy Math Greedy Sorting Geometry Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-29

  • Submissions: Wrong Answer → Time Limit Exceeded → Accepted ×3

Approach
#

After sorting, sides a <= b <= c form a triangle ⇔ a + b > c.

With the longest side fixed at c = nums[i], the best other two sides are the two right before it, nums[i - 1] and nums[i - 2]: the largest values below c, with the largest sum, most likely to exceed c and giving the largest perimeter. If even they fail, smaller ones fail too, so the longest side must move down.

So check consecutive triples from the largest down; the first valid one is the answer.

My Attempts
#

The first two versions used two pointers, one for the longest side and one for the other two, and the pointer moves got tangled:

  • Attempt 1 could count a non-triangle. For [1, 1, 3, 4] no three sides form a triangle, so the answer is 0; it returned 3.
  • Attempt 2 changed the condition, but in some cases neither pointer moved; [8, 8, 1, 7] loops forever, TLE.

Once it is clear that “with the longest side fixed, the two largest others are best”, one loop suffices.

Solution
#

class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
        nums.sort()
        for i in range(len(nums) - 1, 1, -1):
            if nums[i - 2] + nums[i - 1] > nums[i]:
                return nums[i - 2] + nums[i - 1] + nums[i]
        return 0

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • The triangle inequality is strict.
  • No two pointers needed: with the longest side fixed, its two neighbors are always best.

Interview Explanation
#

After sorting, the best partners for a fixed longest side are the two right before it, since their sum is largest. So I check consecutive triples from the largest down, and the first with a + b > c gives the largest perimeter; if none does, return 0. Time O(n log n).

Related

LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 3074: Apple Redistribution into Boxes
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 3074 note: apples can be split freely, so take boxes from the largest capacity until the total fits every apple
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 min
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
LeetCode 2335 note: the answer is max(the largest count, ceil(total / 2)); simulating means filling one cup from each of the two largest every second
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 1005: Maximize Sum Of Array After K Negations
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 1005 note: negate the smallest value each time; once the smallest is positive, only the parity of the remaining count matters