[LeetCode] 3096. Minimum Levels to Gain More Points

CNoodle發表於2024-07-19

You are given a binary array possible of length n.

Alice and Bob are playing a game that consists of n levels. Some of the levels in the game are impossible to clear while others can always be cleared. In particular, if possible[i] == 0, then the ith level is impossible to clear for both the players. A player gains 1 point on clearing a level and loses 1 point if the player fails to clear it.

At the start of the game, Alice will play some levels in the given order starting from the 0th level, after which Bob will play for the rest of the levels.

Alice wants to know the minimum number of levels she should play to gain more points than Bob, if both players play optimally to maximize their points.

Return the minimum number of levels Alice should play to gain more points. If this is not possible, return -1.

Note that each player must play at least 1 level.

Example 1:
Input: possible = [1,0,1,0]
Output: 1

Explanation:
Let's look at all the levels that Alice can play up to:

If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point.
If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.

If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point.
Alice must play a minimum of 1 level to gain more points.

Example 2:
Input: possible = [1,1,1,1,1]
Output: 3

Explanation:
Let's look at all the levels that Alice can play up to:

If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.
If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.
If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.
If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.
Alice must play a minimum of 3 levels to gain more points.

Example 3:
Input: possible = [0,0]
Output: -1

Explanation:
The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can't gain more points than Bob.

Constraints:
2 <= n == possible.length <= 105
possible[i] is either 0 or 1.

得到更多分數的最少關卡數目。

給你一個長度為 n 的二進位制陣列 possible 。

Alice 和 Bob 正在玩一個有 n 個關卡的遊戲,遊戲中有一些關卡是 困難 模式,其他的關卡是 簡單 模式。如果 possible[i] == 0 ,那麼第 i 個關卡是 困難 模式。一個玩家透過一個簡單模式的關卡可以獲得 1 分,透過困難模式的關卡將失去 1 分。

遊戲的一開始,Alice 將從第 0 級開始 按順序 完成一些關卡,然後 Bob 會完成剩下的所有關卡。

假設兩名玩家都採取最優策略,目的是 最大化 自己的得分,Alice 想知道自己 最少 需要完成多少個關卡,才能獲得比 Bob 更多的分數。

請你返回 Alice 獲得比 Bob 更多的分數所需要完成的 最少 關卡數目,如果 無法 達成,那麼返回 -1 。

注意,每個玩家都至少需要完成 1 個關卡。

思路

思路是字首和。Alice 需要確保自己在儘可能少完成關卡的情況下得分比 Bob 高,那麼當 Alice 的得分第一次比 Bob 高的時候,我們就可以停下了,這個位置就是題目要求我們找的位置。

具體的做法是我們需要遍歷 input 陣列兩遍。第一次遍歷的時候統計整個陣列的字首和,記為 sum。第二次遍歷的時候我們一邊遍歷,一邊比較 Alice 和 Bob 的分數差。其中 Bob 在某個 index 的分數 = sum - Alice 在這個 index 停下的分數。注意第二次遍歷的時候 for 迴圈要在 n - 1 停下,因為題目要求每個玩家都至少需要完成 1 個關卡。

複雜度

時間O(n)
空間O(1)

程式碼

Java實現

class Solution {
    public int minimumLevels(int[] possible) {
        int n = possible.length;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int score = possible[i] == 1 ? 1 : -1;
            sum += score;
        }

        int alice = 0;
        for (int i = 0; i < n - 1; i++) {
            int score = possible[i] == 1 ? 1 : -1;
            alice += score;
            int bob = sum - alice;
            if (alice > bob) {
                return i + 1;
            }
        }
        return -1;
    }
}

相關文章