LeetCode #948

Bag of Tokens

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    int bagOfTokensScore(vector<int>& tokens, int power) {
        sort(tokens.begin(), tokens.end());
        int l = 0, r = (int)tokens.size() - 1;
        int score = 0, mx = 0;
        while(l <= r) {
            if(power >= tokens[l]) {
                mx = max(mx, ++ score);
                power -= tokens[l ++];
            }
            else if(score >= 1) {
                -- score;
                power += tokens[r --];
            }
            else break;
        }
        return mx;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.