作者JerryChungYC (JerryChung)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Thu Feb 15 13:49:48 2024
https://leetcode.com/problems/find-polygon-with-the-largest-perimeter
找出能做出最大多邊形邊長的數組,有則回傳周長,沒有則回傳-1
※ 引述《wu10200512 (廷廷)》之銘言:
: 2971. Find Polygon With the Largest Perimeter
Python3 code:
--------------------------------------------------------
from typing import List
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
lst = sorted(nums)
pos = len(lst) - 1
while pos >= 2:
if sum(lst[:pos]) > lst[pos]:
return sum(lst[:pos+1])
pos -= 1
return -1
--------------------------------------------------------
排序後由後往前找
--------------------------------------------------------
from typing import List
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
lst = sorted(nums)
pos = lst.pop()
while len(lst) >= 2:
if sum(lst) > pos:
return sum(lst) + pos
pos = lst.pop()
return -1
--------------------------------------------------------
差不多 只是把pos從記錄位置改成pop當前list的最大值
原本想2月好好解每日 結果過年根本沒碰 我好爛
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.251.109 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1707976191.A.21B.html
推 JIWP: 大師 02/15 13:51
推 sustainer123: 大師 02/15 13:59