Algorithm/NeetCode

Find Polygon With the Largest Perimeter

Tony Lim 2024. 11. 27. 15:30
728x90

https://leetcode.com/problems/find-polygon-with-the-largest-perimeter/

class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
        nums.sort()
        res = -1
        total = 0
        
        for n in nums:
            if total > n:
                res = total + n
            total += n
        return res

once we sort all we have to do is iterate though left to right and check for maximum sum

728x90