LeetCode solutions

3262. Find Polygon With the Largest Perimeter

My accepted Python solution to LeetCode problem 3262, Find Polygon With the Largest Perimeter, running in 68ms.

  • Difficulty: Medium
  • Python
  • Runtime 68ms
  • Memory 31.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 68ms, memory 31.6MB, accepted 2025-12-30.

python
class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
        nums.sort()
        total = sum(nums)
        
        # Start from largest side and check if it can form a polygon
        # A polygon can be formed if the longest side < sum of all other sides
        for i in range(len(nums) - 1, 1, -1):
            total_except_largest = total - nums[i]
            if nums[i] < total_except_largest:
                return total
            total = total_except_largest
        
        return -1

Source