目录
一,题目描述
英文描述
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.
中文描述
你正在玩一个单人游戏,面前放置着大小分别为 a、b 和 c 的 三堆 石子。
每回合你都要从两个 不同的非空堆 中取出一颗石子,并在得分上加 1 分。当存在 两个或更多 的空堆时,游戏停止。
给你三个整数 a 、b 和 c ,返回可以得到的 最大分数 。
示例与说明
来源:力扣(LeetCode)
链接:力扣
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二,解题思路
假设a、b、c分别存放最小值、中间值、最大值。
最终目的是实现两个以上空堆,并保证非空的一堆石子数量尽可能的少。此外:
- 只要a+b<=c,必定可以利用c来将a、b充分消耗;
- 若a+b>c,就可以让a和b互相消耗,从而创造a+b<=c的条件;
三,AC代码
C++
class Solution {
public:
int maximumScore(int a, int b, int c) {
if (a > b) swap(a, b);// a存最小值
if (a > c) swap(a, c);
if (b > c) swap(b, c);// b存次小值
int ans = 0;
while (a + b > c) {
a--;
b--;
ans++;
}
while (a) {
a--;
c--;
ans++;
}
while (b) {
b--;
c--;
ans++;
}
return ans;
}
};
四,解题过程
第一博
一发如魂