> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Perimeter Triangle Python Solution

> Tested Python solution for LeetCode 976 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 976, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/largest-perimeter-triangle/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 976   # by problem number
lcpy gen -s largest_perimeter_triangle   # by problem name
```

## Problem

Given an integer array \<code>nums\</code>, return \<em>the largest perimeter of a triangle with a non-zero area, formed from three of these lengths\</em>. If it is impossible to form any triangle of a non-zero area, return \<code>0\</code>.

### Examples

```
Input: nums = [2,1,2]
Output: 5
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.
```

```
Input: nums = [1,2,1,10]
Output: 0
Explanation: You cannot use the side lengths 1, 1, and 2 to form a triangle.
You cannot use the side lengths 1, 1, and 10 to form a triangle.
You cannot use the side lengths 1, 2, and 10 to form a triangle.
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.
```

### Constraints

* 3 \<= nums.length \<= 10^4
* 1 \<= nums\[i] \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_perimeter_triangle/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_perimeter_triangle/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(1) (sort is in-place for this input list)
    def largest_perimeter(self, nums: list[int]) -> int:
        lengths = sorted(nums, reverse=True)
        for i in range(len(lengths) - 2):
            if lengths[i] < lengths[i + 1] + lengths[i + 2]:
                return lengths[i] + lengths[i + 1] + lengths[i + 2]
        return 0
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(1) (sort is in-place for this input list) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.