Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/11/21 00:02), 編輯推噓1(102)
留言3則, 1人參與, 1年前最新討論串1135/1554 (看更多)
跟大部分人差不多 吧 不過往回找的時候好像可以不用abc都檢查 可以只檢查被扣掉的那個字母的count就好 def takeCharacters(self, s: str, k: int) -> int: cnt = [0 for _ in range(3)] for i in range(len(s)): cnt[ord(s[i])-ord('a')] += 1 if not all([cnt[i]>=k for i in range(3)]): return -1 n = len(s) r = n-1 ans = n for i in range(n): l = n-1-i cnt[ord(s[l])-ord('a')] -= 1 while r>=0 and not all([cnt[i]>=k for i in range(3)]): cnt[ord(s[r])-ord('a')] += 1 r -= 1 ans = min(ans, n-r-1+l) return ans -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1732118544.A.D4A.html

11/21 00:06, 1年前 , 1F
確實 不過只有三個 就懶得弄了
11/21 00:06, 1F

11/21 00:07, 1年前 , 2F
我記得有周賽是26個字母 強制你一定要只檢查當個 不能26
11/21 00:07, 2F

11/21 00:07, 1年前 , 3F
個都檢查 不然超時
11/21 00:07, 3F
文章代碼(AID): #1dFWWGrA (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dFWWGrA (Marginalman)