-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBagOfToken.java
More file actions
77 lines (52 loc) · 2.28 KB
/
Copy pathBagOfToken.java
File metadata and controls
77 lines (52 loc) · 2.28 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.LeetCode;
import java.util.Arrays;
public class BagOfToken {
/*
LeetCode Algorithm Question - Bag of Tokens Solution
Solved Date : 2020-10-25
Author : TK Lee
Source : https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3506/
Question Description :
You have an initial power of P, an initial score of 0, and a bag of tokens where tokens[i] is the value of the ith token (0-indexed).
Your goal is to maximize your total score by potentially playing each token in one of two ways:
If your current power is at least tokens[i], you may play the ith token face up, losing tokens[i] power and gaining 1 score.
If your current score is at least 1, you may play the ith token face down, gaining tokens[i] power and losing 1 score.
Each token may be played at most once and in any order. You do not have to play all the tokens.
Return the largest possible score you can achieve after playing any number of tokens.
*/
public static int bagOfTokensScore(int[] tokens, int P)
{
//Variable Initialization
int[] tokenList = tokens.clone();
int power = P;
int maxScore = 0;
int score = 0;
int curPower = power;
int left = 0;
int right = tokenList.length -1;
//Array Token List
Arrays.sort(tokenList);
while (right >= left) {
//If Power >= Token, subtract token amount from power to again score.
if (curPower >= tokenList[left]){
score++;
curPower -= tokenList[left++];
//Keep Maximized scores
maxScore = Math.max(maxScore, score);
} else if (score >0) {
score--;
curPower += tokenList[right--];
} else {
return maxScore;
}
} ;
return maxScore;
}
public static void main(String[] args){
int[] tokens = {81,91,31};
int p = 73;
int score = 0;
score = bagOfTokensScore(tokens, p);
System.out.println("Score: " + score);
}
}