Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (神楽めあ的錢包)時間1年前 (2024/07/13 01:36), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串482/1552 (看更多)
好久沒po每日文了 最近上班比較忙 不知道還能堅持多久 1717. Maximum Score From Removing Substrings 給一個字串:s,和兩個數字x、y 當你移除字串中的"ab"時可以獲得x分 移除字串中的"ba"時可以獲得y分 請問最多可以獲得幾分? 思路: 去比較x、y誰比較大 x比較大就先處理"ab",反之處理"ba" 接著再去處理剩下的 golang code : func maximumGain(s string, x int, y int) int { b,stack,n:=[]byte(s),[]byte{},len(s) res1:=0 var first,second byte var cnt1 ,cnt2 int if x>y{ first='b' second='a' cnt1=x cnt2=y }else{ first='a' second='b' cnt1=y cnt2=x } for i:=0;i<n;i++{ if b[i]==first && len(stack)>0 && stack[len(stack)-1]==second{ res1+=cnt1 stack=stack[:len(stack)-1] }else{ stack=append(stack,b[i]) } } stack1:=[]byte{} for i:=0;i<len(stack);i++{ if stack[i]==second && len(stack1)>0 && stack1[len(stack1)-1]==first{ res1+=cnt2 stack1=stack1[:len(stack1)-1] }else{ stack1=append(stack1,stack[i]) } } return res1 } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.138.211.18 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720805811.A.81A.html
文章代碼(AID): #1caMcpWQ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1caMcpWQ (Marginalman)