LeetCode-1753. Maximum Score From Removing Stones [Medium]-Analysis and Code (Java)

LeetCode-1753. Maximum Score From Removing Stones [Maximum Score From Removing Stones] [Medium]-Analysis and Code [Java]

1. Topic

You are playing a single player game with three piles of stones of size a, b, and c in front of you.
Each round you have to take a stone from two different non-empty piles and add 1 point to the score. When there are two or more empty piles, the game stops.
Give you three integers a, b, and c, and return the maximum score that can be obtained.

Example 1:

输入:a = 2, b = 4, c = 6
输出:6
解释:石子起始状态是 (2, 4, 6) ,最优的一组操作是:
- 从第一和第三堆取,石子状态现在是 (1, 4, 5)
- 从第一和第三堆取,石子状态现在是 (0, 4, 4)
- 从第二和第三堆取,石子状态现在是 (0, 3, 3)
- 从第二和第三堆取,石子状态现在是 (0, 2, 2)
- 从第二和第三堆取,石子状态现在是 (0, 1, 1)
- 从第二和第三堆取,石子状态现在是 (0, 0, 0)
总分:6 分 。

Example 2:

输入:a = 4, b = 4, c = 6
输出:7
解释:石子起始状态是 (4, 4, 6) ,最优的一组操作是:
- 从第一和第二堆取,石子状态现在是 (3, 3, 6)
- 从第一和第三堆取,石子状态现在是 (2, 3, 5)
- 从第一和第三堆取,石子状态现在是 (1, 3, 4)
- 从第一和第三堆取,石子状态现在是 (0, 3, 3)
- 从第二和第三堆取,石子状态现在是 (0, 2, 2)
- 从第二和第三堆取,石子状态现在是 (0, 1, 1)
- 从第二和第三堆取,石子状态现在是 (0, 0, 0)
总分:7 分 。

Example 3:

输入:a = 1, b = 8, c = 8
输出:8
解释:最优的一组操作是连续从第二和第三堆取 8 回合,直到将它们取空。
注意,由于第二和第三堆已经空了,游戏结束,不能继续从第一堆中取石子。

prompt:

  • 1 <= a, b, c <= 10^5

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/maximum-score-from-removing-stones
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Simulation

(1) Thinking

To simulate the process of taking the stones, for the convenience of discussion, the piles of stones can be reordered from less to more. Then the order of taking the stones after sorting is:

  • 1) When a + b> c, take stones from a and b;
  • 2) Take stones from b and c;
  • 3) Take stones from a and c.

(2) Code

class Solution {
    
    
    public int maximumScore(int a, int b, int c) {
    
    
        int[] num = new int[]{
    
    a, b, c};
        Arrays.sort(num);//排序后,num0 <= num1 <= num2
        int ans = 0;
        while (num[0] > 0 && num[0] + num[1] > num[2]) {
    
    
            ans++;
            num[0]--;
            num[1]--;
        }
        while (num[2] > 0 && num[1] > 0) {
    
    
            ans++;
            num[2]--;
            num[1]--;
        }
        while (num[2] > 0 && num[0] > 0) {
    
    
            ans++;
            num[2]--;
            num[0]--;
        }
        return ans;
    }
}

(3) Results

Execution time: 5 ms, beating 42.54% of users
in all Java submissions ; memory consumption: 35.2 MB, beating 71.22% of users in all Java submissions.

2. Direct calculation

(1) Thinking

The above simulation process is simplified to direct calculation.

(2) Code

class Solution {
    
    
    public int maximumScore(int a, int b, int c) {
    
    
        int[] num = new int[]{
    
    a, b, c};
        Arrays.sort(num);//排序后,num0 <= num1 <= num2
        int ans = 0;
        if (num[0] + num[1] > num[2]) {
    
    
            int get = (num[0] + num[1] - num[2]) >> 1;
            ans += get;
            num[0] -= get;
            num[1] -= get;
        }
        ans += Math.min(num[0] + num[1], num[2]);
        return ans;
    }
}

(3) Results

Execution time: 0 ms, beating 100.00% of users
in all Java submissions ; memory consumption: 35.5 MB, beating 25.97% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/114108069