LeetCode-in-Java

1753. Maximum Score From Removing Stones

Medium

You are playing a solitaire game with three piles of stones of sizes a, b, and c respectively. Each turn you choose two different non-empty piles, take one stone from each, and add 1 point to your score. The game stops when there are fewer than two non-empty piles (meaning there are no more available moves).

Given three integers a, b, and c, return the maximum score you can get.

Example 1:

Input: a = 2, b = 4, c = 6

Output: 6

Explanation: The starting state is (2, 4, 6). One optimal set of moves is:

There are fewer than two non-empty piles, so the game ends. Total: 6 points.

Example 2:

Input: a = 4, b = 4, c = 6

Output: 7

Explanation: The starting state is (4, 4, 6). One optimal set of moves is:

There are fewer than two non-empty piles, so the game ends. Total: 7 points.

Example 3:

Input: a = 1, b = 8, c = 8

Output: 8

Explanation: One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty. After that, there are fewer than two non-empty piles, so the game ends.

Constraints:

Solution

import java.util.Arrays;

public class Solution {
    public int maximumScore(int a, int b, int c) {
        int[] nums = new int[] {a, b, c};
        Arrays.sort(nums);
        if (nums[0] + nums[1] < nums[2]) {
            return nums[0] + nums[1];
        } else {
            return (nums[0] + nums[1] + nums[2]) / 2;
        }
    }
}