Find Polygon With the Largest Perimeter Problem
Description
LeetCode Problem 2971.
You are given an array of positive integers nums of length n.
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
Conversely, if you have k (k >= 3) positive real numbers a_1, a_2, a_3, ..., a_k where a_1 <= a_2 <= a_3 <= ... <= a_k and a_1 + a_2 + a_3 + ... + a_(k-1) > a_k, then there always exists a polygon with k sides whose lengths are a_1, a_2, a_3, ..., a_k.
The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
Example 1:
Input: nums = [5,5,5]
Output: 15
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
Example 2:
Input: nums = [1,12,1,2,5,50,3]
Output: 12
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.
Example 3:
Input: nums = [5,5,50]
Output: -1
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
Constraints:
3 <= n <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Sorting + Prefix Sum
A set of sides forms a polygon exactly when the longest side is shorter than the sum of the others. Sort nums and build prefix sums s. For each k >= 3, take the first k sorted elements: if the largest of them, nums[k - 1], is smaller than the sum of the previous ones s[k - 1], then they form a polygon with perimeter s[k]. Dropping smaller elements only lowers the perimeter, so the answer is the largest such s[k], or -1 if no k works.
The time complexity is O(n log n) and the space complexity is O(n), where n is the length of nums.
from itertools import accumulate
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
nums.sort()
s = list(accumulate(nums, initial=0))
ans = -1
for k in range(3, len(nums) + 1):
if s[k - 1] > nums[k - 1]:
ans = max(ans, s[k])
return ans