From b3b2e70d415c0daac7e490288346e64d30553b44 Mon Sep 17 00:00:00 2001 From: Shahriar Shatil <52494840+ShatilKhan@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:12:43 +0600 Subject: [PATCH] Time: 48 ms (83.74%) | Memory: 16.6 MB (94.31%) - LeetSync --- 985-bag-of-tokens/bag-of-tokens.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 985-bag-of-tokens/bag-of-tokens.py diff --git a/985-bag-of-tokens/bag-of-tokens.py b/985-bag-of-tokens/bag-of-tokens.py new file mode 100644 index 0000000..057bb5a --- /dev/null +++ b/985-bag-of-tokens/bag-of-tokens.py @@ -0,0 +1,19 @@ +class Solution: + def bagOfTokensScore(self, tokens: List[int], power: int) -> int: + res = score = 0 + tokens.sort() + + l, r = 0, len(tokens) - 1 + while l <= r: + if power >= tokens[l]: + power -= tokens[l] + score += 1 + l += 1 + res = max(res, score) + elif score > 0: + power += tokens[r] + r -= 1 + score -= 1 + else: + break + return res \ No newline at end of file