Advertisement

Bag of Tokens - LeetCode 948 Solution

Bag of Tokens - Complete Solution Guide

Bag of Tokens is LeetCode problem 948, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 token i . 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 token i , losing tokens[i] power and gaining 1 score. Face-down : If your c

Detailed Explanation

The problem asks us to maximize the score we can obtain by playing tokens, given an initial power and a list of token values. We can play a token in two ways: face-up (spend power to gain score) or face-down (spend score to gain power). The goal is to find the maximum score we can achieve through strategic token plays.

Solution Approach

The solution uses a greedy approach combined with a two-pointer technique. First, the tokens are sorted in ascending order. Then, two pointers, `left` and `right`, are initialized to the start and end of the sorted token list, respectively. We iterate while `left <= right`. In each iteration, we check if we can play the token at `left` face-up (power >= tokens[left]). If so, we increment the score and reduce the power. Otherwise, if the score is greater than 0 and there are at least two tokens left (left < right), we play the token at `right` face-down, increasing power and decreasing score. If neither of these conditions is met, it means we cannot play any more tokens, and we break the loop. We maintain the maximum score achieved during the process.

Step-by-Step Algorithm

  1. Step 1: Sort the `tokens` array in ascending order.
  2. Step 2: Initialize `score` to 0, `max_score` to 0, `left` to 0, and `right` to `len(tokens) - 1`.
  3. Step 3: While `left <= right`, do the following:
  4. Step 4: If `power >= tokens[left]`, play the token face-up: decrement `power` by `tokens[left]`, increment `score` by 1, increment `left` by 1, and update `max_score` to the maximum of its current value and `score`.
  5. Step 5: Else if `score > 0` and `left < right`, play the token face-down: increment `power` by `tokens[right]`, decrement `score` by 1, and decrement `right` by 1.
  6. Step 6: Else, break the loop because no more moves are possible.
  7. Step 7: Return `max_score`.

Key Insights

  • Insight 1: Sorting the tokens allows us to efficiently use a greedy approach, as we can prioritize playing smaller tokens face-up when we have enough power and larger tokens face-down when our score allows.
  • Insight 2: A two-pointer approach helps optimize the process of deciding which tokens to play face-up or face-down based on current power and score.
  • Insight 3: The problem requires careful handling of edge cases such as not having enough power or score to play any tokens, or when there are no tokens at all.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Greedy, Sorting.

Companies

Asked at: Flexport.