Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Bag of Tokens

Posted on June 18, 2019July 26, 2020 by braindenny

Bag of Tokens



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #twopointer, #greedy

You have an initial power P, an initial score of 0 points, and a bag of tokens.

Each token can be used at most once, has a value token[i], and has potentially two ways to use it.

  • If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
  • If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.

Return the largest number of points we can have after playing any number of tokens.

Example 1:

Input: tokens = [100], P = 50
Output: 0

Example 2:

Input: tokens = [100,200], P = 150
Output: 1

Example 3:

Input: tokens = [100,200,300,400], P = 200
Output: 2

Note:

  1. tokens.length <= 1000
  2. 0 <= tokens[i] < 10000
  3. 0 <= P < 10000

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/bag-of-tokens
// Basic Ideas: greedy + two pointer
//
// get as many power points as possible with cheap price
// trade one points with high token
//
// Notice: When to stop power -> token?
//
// Complexity: Time O(n*log(n)), Space O(1)
import "sort"
func max(x, y int) int {
    if x>y {
        return x
    } else {
        return y
    }
}

func bagOfTokensScore(tokens []int, P int) int {
    res := 0
    power, token := 0, P
    sort.Ints(tokens)
    left, right := 0, len(tokens)-1

    // fmt.Println(tokens)
    // token >= tokens[left] || power>0: can make a deal
    for left <= right && (token >= tokens[left] || power>0) {
        // get as many power points as possible with cheap price
        for left <= right && token >= tokens[left] {
            token -= tokens[left]
            power++
            left++
            res = max(res, power)
        }
        // trade one points with high token
        if left<=right && power>0 {
            token += tokens[right]
            power--
            right--
        }
        // fmt.Println(res, power, token, left, right)
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in BasicTagged #greedy, #twopointer

Post navigation

LeetCode: Delete Columns to Make Sorted II
LeetCode: Most Stones Removed with Same Row or Column

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.