天天看点

LeetCode_Sorting_1753. Maximum Score From Removing Stones 移除石子的最大得分【脑筋急转弯】【C++】【中等】

目录

​​一,题目描述​​

​​英文描述​​

​​中文描述​​

​​示例与说明​​

​​二,解题思路​​

​​三,AC代码​​

​​C++​​

​​四,解题过程​​

​​第一博​​

一,题目描述

英文描述

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_Sorting_1753. Maximum Score From Removing Stones 移除石子的最大得分【脑筋急转弯】【C++】【中等】
LeetCode_Sorting_1753. Maximum Score From Removing Stones 移除石子的最大得分【脑筋急转弯】【C++】【中等】
LeetCode_Sorting_1753. Maximum Score From Removing Stones 移除石子的最大得分【脑筋急转弯】【C++】【中等】

二,解题思路

假设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;
    }
};      

四,解题过程

第一博