Re: [閒聊] 每日leetcode
今天每日
948. Bags of tokens
給你一串陣列跟power
可以對陣列中的數字做兩種操作
1.
用power去減那個數字 換一分
2.
減一分 獲得那個數字的power
解法:
所以最好是要用power 減最少的數字
得到一分
然後缺power的時候再去找最大的數字
用一分換到最多power
所以sort是一定要的
然後用two pointers確定目前進度
解出來ㄌ
ya
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power)
{
sort(tokens.begin(),tokens.end(),less());
int maxscore = 0;
int scon = 0;
int len = tokens.size();
int r = len-1;
int l = 0;
while(l<=r)
{
while((l<=r)&&(tokens[l] <= power))
{
power -= tokens[l];
l++;
scon++;
maxscore = max(scon , maxscore);
}
power += tokens[r];
scon --;
r --;
if(scon < 0)
{
break;
}
}
return maxscore;
}
};
--
邊版的小母雞 — fuckchicken
https://i.imgur.com/wglAuYR.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 134.208.57.64 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709525551.A.FF5.html
噓
03/04 12:12,
1年前
, 1F
03/04 12:12, 1F
推
03/04 12:17,
1年前
, 2F
03/04 12:17, 2F
→
03/04 12:18,
1年前
, 3F
03/04 12:18, 3F
推
03/04 18:08,
1年前
, 4F
03/04 18:08, 4F
討論串 (同標題文章)
完整討論串 (本文為第 17 之 1548 篇):