Re: [閒聊] 每日leetcode已回收
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/03/04 23:10)推噓0(0推 0噓 2→)留言2則, 2人參與討論串18/1548 (看更多)
948. Bag of Tokens
一開始有點卡
不過突然靈光一閃好像是two pointer吼
有的換score就換
沒得換就拿score去換最大的power
持續更新最大值
不過中間還是有WA幾次
像是一開始用到<沒用<= 之類的 有些小細節
int bagOfTokensScore(vector<int>& tokens, int power) {
sort(tokens.begin(), tokens.end());
int l = 0;
int r = tokens.size()-1;
int score = 0;
int ans = 0;
while(l <= r)
{
if(power >= tokens[l])
{
score += 1;
power -= tokens[l];
l += 1;
}
else if(score > 0)
{
score -= 1;
power += tokens[r];
r -= 1;
}
else
{
break;
}
ans = max(ans, score);
}
return ans;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.199.178 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709565018.A.F97.html
→
03/04 23:19,
1年前
, 1F
03/04 23:19, 1F
→
03/04 23:20,
1年前
, 2F
03/04 23:20, 2F
討論串 (同標題文章)
完整討論串 (本文為第 18 之 1548 篇):