1. 程式人生 > >LeetCode 948. Bag of Tokens

LeetCode 948. Bag of Tokens

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
class Solution {
public:
    int bagOfTokensScore(vector<int>& tokens, int p) {
        if(tokens.size()==0)
            return 0;
        sort(tokens.begin(),tokens.end());
        if(tokens[0]>p)
            return 0;
        int score=0;
        //p-=tokens[0];
        //++score;
        int i=0,j=tokens.size()-1,maxx=1;
        while(i<j)
        {
            while(p>=tokens[i])
            {
                p-=tokens[i];
                ++score;
                ++i;
                maxx=max(maxx,score);
            }
            if(i<j)
            {
                p+=tokens[j];
                --score;
                --j;
            }
        }
        return maxx;
    }
};

相關推薦

LeetCode 948. Bag of Tokens

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]

LeetCode948. Bag of Tokens 解題報告(Python)

作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 貪心演算法 日期

948. Bag of Tokens

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&nb

LeetCode周賽#112 Q4 Bag of Tokens(貪心)

題目來源:https://leetcode.com/contest/weekly-contest-112/problems/bag-of-tokens/ 問題描述  948. Bag of Tokens You have an initial power P, an

[Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens

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

[LeetCode]160.Intersection of Two Linked Lists

col style return tro nod sts diff original you Intersection of Two Linked Lists Write a program to find the node at which the intersectio

leetcode 576. Out of Boundary Paths

long pro 圖像 經典的 path 時間 。。 數組元素 con https://leetcode.com/problems/out-of-boundary-paths/#/description 題意大概就是在一個m*n的網格中,在坐標為[i,j]的網格上放一個物體

[leetcode-543-Diameter of Binary Tree]

iam ret class 二叉樹 topic eight tco diameter amp Given a binary tree, you need to compute the length of the diameter of the tree. The dia

第十一周 Leetcode 576. Out of Boundary Paths (HARD) 計數dp

位置 左右 for .com find tps https con light Leetcode 576 給定一個二維平面, 一個球在初始位置(i,j)每次可以轉移到上下左右的一格。 問在N次轉移內,有多少種路徑可以轉移出邊境。 dp[i][j][k]為 在點(i,j) 已

[leetcode-191-Number of 1 Bits]

and 右移 urn returns style this spa mingw -s Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has (als

LeetCode 004 Median of Two Sorted Arrays - Java

min -type port 尋找 style 得到 over size cti There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th

LeetCode 231:Power of Two

data con int style 是否 false 循環 mod 二進制 ?? Given an integer, write a function to determine if it is a power of two. //題目要求:求一個數是否是2的冪次方

Leetcode 233 Number of Digit One

solution lee -m 依據 1.8 leet 2.4 ret general 1. 問題描寫敘述   給定一個整數n,在全部不大於n的非負整數中,計算包括數字1的整數的個數。比如n=13n = 13的結果為6。包括1的數字有1,10,11,1

LeetCode 543. Diameter of Binary Tree (二叉樹的直徑)

tween res edge public level 距離 sent java dot Given a binary tree, you need to compute the length of the diameter of the tree. The diamet

LeetCode】median of two sorted arrays

個數 i++ target 們的 data- 關於 arrays 推斷 get 題目:median of two sorted arrays 知識點:二分查找,中位數定義 public class Solution { /* * 關於:leetco

[leetcode-404-Sum of Left Leaves]

葉子 fin esp binary cnblogs values tree roo col Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 /

[leetcode-200-Number of Islands]

class col color log char ace sum edge site Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is

LeetCode 233. Number of Digit One

tdi pan follow return number mil sta etc family Given an integer n, count the total number of digit 1 appearing in all non-negative integ

[LeetCode] Maximum Product of Three Numbers

clas style find leet 數組元素 cat 兩個 bit bsp Given an integer array, find three numbers whose product is maximum and output the maximum produ

[LeetCode] Exclusive Time of Functions 函數的獨家時間

eem urn run always ati exec led 函數 tput Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU