leetcode 1573
Contents
Leetcode 1753, couldn’t get the solution
Leetcode 1753
Solution
Greedy
Assume a <= b <= c, so we have two conditions:
- a + b <= c, then answer is a + b;
- a + b >= c, then c always choose the larger one of a and b. Finally, a and b will became equal or different by 1. So a and c match k1 times, b and c match k2 times. k1 + k2 = c. The answer should be (k1 + k2) + ((a - k1) + (b - k1)) / 2
code
class Solution {
public:
int maximumScore(int a, int b, int c) {
int sum = a + b + c;
int maxVal = max({a, b, c});
if (sum - maxVal < maxVal) {
return sum - maxVal;
} else {
return sum / 2;
}
}
};