Leetcode 948. Bag of Tokens
- Type: Array (greedy)
- Approach:
- Sort the array.
- Use greedy method to traverse each token from small to large. Every time we would like smallest value in the tokens (deque.popleft()) to waste in power and largest value of tokens (deque.pop()) to add the power.
- Use collections.deque to make tokens a deque so that it can be popped the top value like a queue.
- Complexity:
- Time: O(nlogn)
- Space: O(n)