본문 바로가기
LeetCode/Two Pointers

[LeetCod][Kotlin] 948. Bag of Tokens

by jinwo_o 2024. 11. 14.

948. Bag of Tokens

You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] denotes the value of tokeni.

 

Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):

  • Face-up: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
  • Face-down: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.

Return the maximum possible score you can achieve after playing any number of tokens.

초기 파워는 power, 초기 스코어는 0, 토큰의 가방은 정수 배열 tokens 로서 주어집니다. 각 tokens[i] 는 tokeni 의 값을 나타냅니다.

목표는 이러한 토큰을 전략적으로 플레이하여 총 점수를 극대화하는 것입니다. 한 번의 이동으로 두 가지 방법 중 하나로 플레이되지 않은 토큰을 플레이 할 수 있습니다 (그러나 동일한 토큰에서 플레이 할 수는 없습니다).
- 앞으로: 현재 파워가 tokens[i] 이상인 경우, tokeni 를 플레이하여 tokens[i] 파워를 잃고 스코어를 1 획득할 수 있습니다.
- 뒤로: 현재 점수가 1 이상인 경우 tokeni를 플레이하여 tokens[i] 파워를 획득하여 점수를 1 잃을 수 있습니다.

임의의 수의 토큰을 플레이한 후에 달성할 수 있는 최대 점수를 반환합니다.

 

Example 1:

Input: tokens = [100], power = 50

Output: 0

Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).

 

Example 2:

Input: tokens = [200,100], power = 150

Output: 1

Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.

There is no need to play token0, since you cannot play it face-up to add to your score. The maximum score achievable is 1.

 

Example 3:

Input: tokens = [100,200,300,400], power = 200

Output: 2

Explanation: Play the tokens in this order to get a score of 2:

  1. Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
  2. Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
  3. Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
  4. Play token2 (300) face-up, reducing power to 0 and increasing score to 2.

The maximum score achievable is 2.

 

Constraints:

  • 0 <= tokens.length <= 1000
  • 0 <= tokens[i], power < 10^4

코드 1

class Solution {
    fun bagOfTokensScore(tokens: IntArray, power: Int): Int {
        tokens.sort()

        var answer = 0
        var power = power
        var l = 0
        var r = tokens.lastIndex

        while (l <= r) {
            if (tokens[l] <= power) {
                power -= tokens[l]
                answer++
                l++
            } else if (l != r && answer > 0) {
                power += tokens[r]
                answer--
                r--
            } else {
                break
            }
        }

        return answer
    }
}

 

코드 2

class Solution {
    fun bagOfTokensScore(tokens: IntArray, power: Int): Int {
        tokens.sort()
        
        var res = 0
        var score = 0
        var power = power
        var l = 0
        var r = tokens.lastIndex

        while (l <= r) {
            if (tokens[l] <= power) {
                power -= tokens[l]
                score++
                res = maxOf(res, score)
                l++
            } else if (score > 0) {
                power += tokens[r]
                score--
                r--
            } else {
                break
            }
        }

        return res
    }
}