LeetCode //C - 948. Bag of Tokens

948. Bag of Tokens

You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] denotes the value of tokeni.

Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):

  • Face-up: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
  • Face-down: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.

Return the maximum possible score you can achieve after playing any number of tokens.

 

Example 1:

Input: tokens = [100], power = 50
Output: 0
Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).

Example 2:

Input: tokens = [200,100], power = 150
Output: 1
Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.

Example 3:

Input: tokens = [100,200,300,400], power = 200
Output: 2
Explanation: Play the tokens in this order to get a score of 2:

  1. Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
  2. Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
  3. Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
  4. Play token2 (300) face-up, reducing power to 0 and increasing score to 2.
    The maximum score achievable is 2.
Constraints:
  • 0 <= tokens.length <= 1000
  • 0 < = t o k e n s [ i ] , p o w e r < 1 0 4 0 <= tokens[i], power < 10^4 0<=tokens[i],power<104

From: LeetCode
Link: 948. Bag of Tokens


Solution:

Ideas:
  • Sort tokens in ascending order.

  • Use two indices:

    • left for the smallest token (good for playing face-up to gain score cheaply).
    • right for the largest token (good for playing face-down to regain a lot of power).
  • While possible:

    • If you have enough power for tokens[left], play it face-up:
      power -= tokens[left]; score++; left++;
      Track the maximum score.
    • Else, if you have at least 1 score and left < right, play tokens[right] face-down:
      power += tokens[right]; score–; right–;
    • Otherwise, you cannot improve anymore, so stop.
Code:
int cmpInt(const void *a, const void *b) {
    int x = *(const int *)a;
    int y = *(const int *)b;
    return (x > y) - (x < y);  // equivalent to x - y but safe
}

int bagOfTokensScore(int* tokens, int tokensSize, int power) {
    if (tokensSize == 0) return 0;

    // Sort tokens in ascending order
    qsort(tokens, tokensSize, sizeof(int), cmpInt);

    int left = 0;
    int right = tokensSize - 1;
    int score = 0;
    int maxScore = 0;

    while (left <= right) {
        if (power >= tokens[left]) {
            // Play smallest token face-up
            power -= tokens[left++];
            score++;
            if (score > maxScore) {
                maxScore = score;
            }
        } else if (score > 0 && left < right) {
            // Not enough power: trade score for power using largest token
            power += tokens[right--];
            score--;
        } else {
            // Can't do anything useful anymore
            break;
        }
    }

    return maxScore;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值