Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-03-04 12:12:29
今天每日
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
作者: JIWP (JIWP)   2023-03-04 12:12:00
臭甲
作者: DJYOSHITAKA (Evans)   2024-03-04 12:17:00
大濕
作者: zoeredbird (柔依‧紅鳥)   2024-03-04 12:18:00
甲甲甲甲甲甲

Links booklink

Contact Us: admin [ a t ] ucptt.com