leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 11fbc78c9ecd469905023378c0682a2f23023b52
parent 7a6aa671fd415d0207b96b9c9466214afa33d46f
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon,  4 Mar 2024 14:35:33 +0000

Daily Problem

Diffstat:
AProblems/0948.cpp | 18++++++++++++++++++
MREADME.md | 1+
2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/Problems/0948.cpp b/Problems/0948.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + int bagOfTokensScore(vector<int> &tokens, int power) const { + sort(begin(tokens), end(tokens)); + + const int n = size(tokens); + int score = 0, i = 0, j = n - 1; + while (i <= j) { + if (power >= tokens[i]) + score++, power -= tokens[i++]; + else if (i < j && score > 0) + score--, power += tokens[j--]; + else + return score; + } + return score; + } +}; diff --git a/README.md b/README.md @@ -550,6 +550,7 @@ for solving problems. | 0944 | Easy | [Delete Columns to Make Sorted](Problems/0944.cpp) | | 0946 | Medium | [Validate Stack Sequences](Problems/0946.cpp) | | 0947 | Medium | [Most Stones Removed with Same Row or Column](Problems/0947.cpp) | +| 0948 | Medium | [Bag of Tokens](Problems/0948.cpp) | | 0950 | Medium | [Reveal Cards In Increasing Order](Problems/0950.cpp) | | 0951 | Medium | [Flip Equivalent Binary Trees](Problems/0951.cpp) | | 0953 | Easy | [Verifying an Alien Dictionary](Problems/0953.cpp) |