Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/12/11 22:56), 編輯推噓5(500)
留言5則, 5人參與, 1年前最新討論串1198/1554 (看更多)
今天的 def maximumBeauty(self, nums: List[int], k: int) -> int: tmp = [] for num in nums: tmp.append((num-k, -1)) tmp.append((num+k, 1)) ans, sum = 0, 0 for _, i in sorted(tmp): sum += i ans = max(ans, -sum) return ans 結果很慢 用普通先sort再two pointer好像快很多 昨天的 看提示說數字很小 直覺想到binary search 但寫的好醜 很多地方可以改 但過了就懶改了 def maximumLength(self, s: str) -> int: n = len(s) def check(k): if k==0: return True cnt = defaultdict(int) for i in range(n-k+1): if len(set([c for c in s[i:i+k]]))==1: cnt[s[i]] += 1 if len(cnt)>0 and max(cnt.values())>=3: return True else: return False l,r = 0, n while l<r: mid = (l+r)//2 if not check(mid): r=mid else: l=mid+1 return -1 if l==1 else l-1 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1733928967.A.DD4.html

12/11 23:01, 1年前 , 1F
大師
12/11 23:01, 1F

12/11 23:04, 1年前 , 2F
大師
12/11 23:04, 2F

12/11 23:07, 1年前 , 3F
別卷了
12/11 23:07, 3F

12/11 23:18, 1年前 , 4F
大師
12/11 23:18, 4F

12/12 10:48, 1年前 , 5F
大師
12/12 10:48, 5F
文章代碼(AID): #1dMQW7tK (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dMQW7tK (Marginalman)