Re: [閒聊] 每日LeetCode
看板Marginalman作者JerryChungYC (JerryChung)時間1年前 (2024/02/15 13:49)推噓2(2推 0噓 0→)留言2則, 2人參與討論串680/719 (看更多)
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
推
02/15 13:51,
1年前
, 1F
02/15 13:51, 1F
推
02/15 13:59,
1年前
, 2F
02/15 13:59, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 680 之 719 篇):