bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 948. Bag of Tokens

948. Bag of Tokens

Leetcode Solutions

Greedy Approach with Sorting

  1. Sort the tokens array.
  2. Initialize two pointers, left and right, to the start and end of the array respectively.
  3. Initialize score and maxScore to 0.
  4. While left <= right, repeat the following steps: a. While left <= right and power >= tokens[left], play the left token face up: increase score by 1, decrease power by tokens[left], and increment left. b. Update maxScore to the maximum of maxScore and score. c. If score > 0 and left <= right, play the right token face down: decrease score by 1, increase power by tokens[right], and decrement right.
  5. Return maxScore as the largest possible score.
UML Thumbnail

Brute Force Approach with Backtracking

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR