Bag of Tokens Solutions in C++
Number 948
Difficulty Medium
Acceptance 40.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/bag-of-tokens//// Author : liuyubobobo/// Time : 2018-11-25#include <iostream>#include <vector>using namespace std;/// Greedy Simulation/// Time Complexity: O(nlogn)/// Space Complexity: O(1)class Solution {public:int bagOfTokensScore(vector<int>& tokens, int P) {if(tokens.size() == 0)return 0;sort(tokens.begin(), tokens.end());int res = 0, l = 0, r = tokens.size() - 1;while(true){if(P >= tokens[l]){P -= tokens[l++];res ++;}else {if (l < r && res) {P += tokens[r--];res--;}else if (l == r || !res)break;}if(l > r)break;}return res;}};int main() {vector<int> tokens1 = {100};cout << Solution().bagOfTokensScore(tokens1, 50) << endl;// 0vector<int> tokens2 = {100, 200};cout << Solution().bagOfTokensScore(tokens2, 150) << endl;// 1vector<int> tokens3 = {100, 200, 300, 400};cout << Solution().bagOfTokensScore(tokens3, 200) << endl;// 2vector<int> tokens4 = {71, 55, 82};cout << Solution().bagOfTokensScore(tokens4, 54) << endl;// 0return 0;}