[B] [C] [D]

You are playing a single player game and are presented with three piles of pebbles of size A, B and C.

Each round you take a stone from two different non-empty piles and add one point to your score. The game stops when there are two or more empty heaps.

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 initial state of the pebble is (2, 4, 6), and the optimal operation is: So let's take the pebbles from the first and third heaps, and now we have (1, 4, 5) pebbles from the first and third heaps, so let's take the pebbles from the second and third heaps, So we're going to have (0, 2, 2) -- we're going to have (0, 1, 1) -- we're going to have (0, 0, 0) -- we're going to have (0, 0, 0).Copy the code

Example 2:

Input: A = 4, b = 4, c = 6 Output: 7 Explanation: The initial state of the pebble is (4, 4, 6), and the optimal operation is: So let's take the pebbles from the first and second heaps, and now we have (3, 3, 6) -- let's take the pebbles from the first and third heaps, So we're going to have (0, 3, 3) -- we're going to have (0, 2, 2) -- we're going to have (0, 1, 1) -- we're going to have (0, 0, 0) -- we're going to have (0, 0, 0).Copy the code

Example 3:

Input: A = 1, b = 8, C = 8 Output: 8 Explanation: The optimal set of operations is to take 8 consecutive turns from the second and third heap until they are empty. Notice that since the second and third piles are empty, the game is over and we can't continue to take pebbles from the first pile.Copy the code

 

Tip:

  • 1 <= a, b, c <= 105

Their thinking

In this problem, we do not need to solve the process of optimal operation according to the number of inputs, but only need to judge a mutual relationship between the three numbers of inputs to solve the problem.

  1. If the sum of the maximum heap is greater than or equal to the sum of the other two piles, then the best operation is to take one stone from the maximum heap and combine it with the stones of the other two piles, and the maximum score can be obtained is the sum of the smaller two piles of stones
  2. If the sum of the maximum heaps is less than the sum of the other two heaps, then only one pebble will be left at most after reasonable operation (that is, if the sum of the three heaps is odd), so the maximum score is equal to the sum of the three heapssum/2Take down the whole

Code implementation

var maximumScore = function(a, b, C) {const arr = [a,b,c] arr.sort((a,b) => a-b) const sum = arr[0]+arr[1 If (sum<=arr[2]){return sum; Return (sum+arr[2])>>1}}; return (sum+arr[2])>>1}};Copy the code

Now we have leetcode-1753- the maximum score for removing stones

If you have any questions or suggestions, please leave a comment!