作者DJYOSHITAKA (franchouchouISBEST)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Tue May 28 23:59:32 2024
: 1208. Get Equal Substrings Within Budget
每次two pointer或像是binary search的東西
都靠感覺亂想
感覺應該要有一個自己的格式
不然會卡卡的:(
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
l=0
cost_cur=0
ans=0
for r in range(len(s)):
cost_cur += abs(ord(s[r])-ord(t[r]))
while l<=r and cost_cur > maxCost:
cost_cur -= abs(ord(s[l])-ord(t[l]))
l += 1
ans = max(r-l+1, ans)
return ans
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.79.34.96 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1716911975.A.0B2.html
推 RinNoKareshi: 寶 別卷了 05/29 00:00
推 sustainer123: 我理解two pointer就兩個形式 快慢指針跟左右 05/29 00:02
→ sustainer123: binary search我本來覺得很簡單 05/29 00:03
→ sustainer123: 實際刷題就很難想到 一堆感覺無序數列都能用 05/29 00:03
→ rainkaras: 寶 你好猛 05/29 00:11