Re: [閒聊] 每日LeetCode

看板Marginalman作者 (JerryChung)時間1年前 (2024/02/15 13:49), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串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
文章代碼(AID): #1bpQN_8R (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bpQN_8R (Marginalman)