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.
At each step, to maximize the score, always remove stones from the two largest non-empty piles. The process stops when at most one pile is non-empty. The answer is the minimum of (a + b + c) // 2 and a + b + c - max(a, b, c).
classSolution {
public:int maximumScore(int a, int b, int c) {
vector<int> v = {a, b, c};
sort(v.begin(), v.end());
if (v[2] >= v[0] + v[1]) return v[0] + v[1];
return (a + b + c) /2;
}
};
classSolution {
publicintmaximumScore(int a, int b, int c) {
int[] v = {a, b, c};
Arrays.sort(v);
if (v[2]>= v[0]+ v[1]) return v[0]+ v[1];
return (a + b + c) / 2;
}
}
1
2
3
4
5
6
classSolution {
funmaximumScore(a: Int, b: Int, c: Int): Int {
val v = listOf(a, b, c).sorted()
returnif (v[2] >= v[0] + v[1]) v[0] + v[1] else (a + b + c) / 2 }
}
1
2
3
4
5
6
classSolution:
defmaximumScore(self, a: int, b: int, c: int) -> int:
v = sorted([a, b, c])
if v[2] >= v[0] + v[1]:
return v[0] + v[1]
return (a + b + c) //2
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnmaximum_score(a: i32, b: i32, c: i32) -> i32 {
letmut v =vec![a, b, c];
v.sort();
if v[2] >= v[0] + v[1] {
v[0] + v[1]
} else {
(a + b + c) /2 }
}
}