Skip to content

Commit 3eb0bda

Browse files
authored
Create 2971-find-polygon-with-the-largest-perimeter.py
1 parent 7947052 commit 3eb0bda

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time complexity O(nlogn)
2+
class Solution:
3+
def largestPerimeter(self, nums: List[int]) -> int:
4+
nums.sort()
5+
res = -1
6+
total = 0
7+
8+
for n in nums:
9+
if total > n:
10+
res = total + n
11+
total += n
12+
13+
return res
14+
15+
# Time complexity O(n + 30logn) ~ O(n)
16+
class Solution:
17+
def largestPerimeter(self, nums: List[int]) -> int:
18+
curSum = sum(nums)
19+
heapq._heapify_max(nums)
20+
21+
while nums and curSum <= nums[0] * 2:
22+
curSum -= heapq._heappop_max(nums)
23+
24+
return curSum if len(nums) > 2 else -1
25+

0 commit comments

Comments
 (0)