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