-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution948.cs
43 lines (39 loc) · 980 Bytes
/
Solution948.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution948
{
/// <summary>
/// 948. Bag of Tokens - Medium
/// <a href="https://leetcode.com/problems/bag-of-tokens">See the problem</a>
/// </summary>
public int BagOfTokensScore(int[] tokens, int power)
{
Array.Sort(tokens);
var left = 0;
var right = tokens.Length - 1;
var score = 0;
var maxScore = 0;
while (left <= right)
{
if (power >= tokens[left])
{
power -= tokens[left];
score++;
maxScore = Math.Max(maxScore, score);
left++;
}
else if (score > 0)
{
power += tokens[right];
score--;
right--;
}
else
{
break;
}
}
return maxScore;
}
}