Skip to content

Commit b8e5504

Browse files
Create README - LeetHub
1 parent a1c1889 commit b8e5504

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

0948-bag-of-tokens/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<h2><a href="https://leetcode.com/problems/bag-of-tokens/">948. Bag of Tokens</a></h2><h3>Medium</h3><hr><div><p>You start with an initial <strong>power</strong> of <code>power</code>, an initial <strong>score</strong> of <code>0</code>, and a bag of tokens given as an integer array <code>tokens</code>, where each&nbsp;<code>tokens[i]</code> donates the value of token<em><sub>i</sub></em>.</p>
2+
3+
<p>Your goal is to <strong>maximize</strong> the total <strong>score</strong> by strategically playing these tokens. In one move, you can play an <strong>unplayed</strong> token in one of the two ways (but not both for the same token):</p>
4+
5+
<ul>
6+
<li><strong>Face-up</strong>: If your current power is <strong>at least</strong> <code>tokens[i]</code>, you may play token<em><sub>i</sub></em>, losing <code>tokens[i]</code> power and gaining <code>1</code> score.</li>
7+
<li><strong>Face-down</strong>: If your current score is <strong>at least</strong> <code>1</code>, you may play token<em><sub>i</sub></em>, gaining <code>tokens[i]</code> power and losing <code>1</code> score.</li>
8+
</ul>
9+
10+
<p>Return <em>the <strong>maximum</strong> possible score you can achieve after playing <strong>any</strong> number of tokens</em>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<div class="example-block" style="
16+
border-color: var(--border-tertiary);
17+
border-left-width: 2px;
18+
color: var(--text-secondary);
19+
font-size: .875rem;
20+
margin-bottom: 1rem;
21+
margin-top: 1rem;
22+
overflow: visible;
23+
padding-left: 1rem;
24+
">
25+
<p><strong>Input:</strong> <span class="example-io" style="
26+
font-family: Menlo,sans-serif;
27+
font-size: 0.85rem;
28+
">tokens = [100], power = 50</span></p>
29+
30+
<p><strong>Output:</strong> <span class="example-io" style="
31+
font-family: Menlo,sans-serif;
32+
font-size: 0.85rem;
33+
">0</span></p>
34+
35+
<p><strong>Explanation</strong><strong>:</strong> Since your score is <code>0</code> initially, you cannot play the token face-down. You also cannot play it face-up since your power (<code>50</code>) is less than <code>tokens[0]</code>&nbsp;(<code>100</code>).</p>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block" style="
41+
border-color: var(--border-tertiary);
42+
border-left-width: 2px;
43+
color: var(--text-secondary);
44+
font-size: .875rem;
45+
margin-bottom: 1rem;
46+
margin-top: 1rem;
47+
overflow: visible;
48+
padding-left: 1rem;
49+
">
50+
<p><strong>Input:</strong> <span class="example-io" style="
51+
font-family: Menlo,sans-serif;
52+
font-size: 0.85rem;
53+
">tokens = [200,100], power = 150</span></p>
54+
55+
<p><strong>Output:</strong> <span class="example-io" style="
56+
font-family: Menlo,sans-serif;
57+
font-size: 0.85rem;
58+
">1</span></p>
59+
60+
<p><strong>Explanation:</strong> Play token<em><sub>1</sub></em> (<code>100</code>) face-up, reducing your power to&nbsp;<code>50</code> and increasing your score to&nbsp;<code>1</code>.</p>
61+
62+
<p>There is no need to play token<em><sub>0</sub></em>, since you cannot play it face-up to add to your score. The maximum score achievable is <code>1</code>.</p>
63+
</div>
64+
65+
<p><strong class="example">Example 3:</strong></p>
66+
67+
<div class="example-block" style="
68+
border-color: var(--border-tertiary);
69+
border-left-width: 2px;
70+
color: var(--text-secondary);
71+
font-size: .875rem;
72+
margin-bottom: 1rem;
73+
margin-top: 1rem;
74+
overflow: visible;
75+
padding-left: 1rem;
76+
">
77+
<p><strong>Input:</strong> <span class="example-io" style="
78+
font-family: Menlo,sans-serif;
79+
font-size: 0.85rem;
80+
">tokens = [100,200,300,400], power = 200</span></p>
81+
82+
<p><strong>Output:</strong> <span class="example-io" style="
83+
font-family: Menlo,sans-serif;
84+
font-size: 0.85rem;
85+
">2</span></p>
86+
87+
<p><strong>Explanation:</strong> Play the tokens in this order to get a score of <code>2</code>:</p>
88+
89+
<ol>
90+
<li>Play token<em><sub>0</sub></em> (<code>100</code>) face-up, reducing power to <code>100</code> and increasing score to <code>1</code>.</li>
91+
<li>Play token<em><sub>3</sub></em> (<code>400</code>) face-down, increasing power to <code>500</code> and reducing score to <code>0</code>.</li>
92+
<li>Play token<em><sub>1</sub></em> (<code>200</code>) face-up, reducing power to <code>300</code> and increasing score to <code>1</code>.</li>
93+
<li>Play token<em><sub>2</sub></em> (<code>300</code>) face-up, reducing power to <code>0</code> and increasing score to <code>2</code>.</li>
94+
</ol>
95+
96+
<p><span style="color: var(--text-secondary); font-size: 0.875rem;">The maximum score achievable is </span><code style="color: var(--text-secondary); font-size: 0.875rem;">2</code><span style="color: var(--text-secondary); font-size: 0.875rem;">.</span></p>
97+
</div>
98+
99+
<p>&nbsp;</p>
100+
<p><strong>Constraints:</strong></p>
101+
102+
<ul>
103+
<li><code>0 &lt;= tokens.length &lt;= 1000</code></li>
104+
<li><code>0 &lt;= tokens[i], power &lt; 10<sup>4</sup></code></li>
105+
</ul>
106+
</div>

0 commit comments

Comments
 (0)