首页 > 其他分享 >[LeetCode] 1753. Maximum Score From Removing Stones

[LeetCode] 1753. Maximum Score From Removing Stones

时间:2022-12-22 02:55:05浏览次数:61  
标签:Stones 3rd piles Removing Maximum 2nd state Take now

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.

Example 1:

Input: a = 2, b = 4, c = 6
Output: 6
Explanation: The starting state is (2, 4, 6). One optimal set of moves is:
- Take from 1st and 3rd piles, state is now (1, 4, 5)
- Take from 1st and 3rd piles, state is now (0, 4, 4)
- Take from 2nd and 3rd piles, state is now (0, 3, 3)
- Take from 2nd and 3rd piles, state is now (0, 2, 2)
- Take from 2nd and 3rd piles, state is now (0, 1, 1)
- Take from 2nd and 3rd piles, state is now (0, 0, 0)
There are fewer than two non-empty piles, so the game ends. Total: 6 points.

Example 2:

Input: a = 4, b = 4, c = 6
Output: 7
Explanation: The starting state is (4, 4, 6). One optimal set of moves is:
- Take from 1st and 2nd piles, state is now (3, 3, 6)
- Take from 1st and 3rd piles, state is now (2, 3, 5)
- Take from 1st and 3rd piles, state is now (1, 3, 4)
- Take from 1st and 3rd piles, state is now (0, 3, 3)
- Take from 2nd and 3rd piles, state is now (0, 2, 2)
- Take from 2nd and 3rd piles, state is now (0, 1, 1)
- Take from 2nd and 3rd piles, state is now (0, 0, 0)
There are fewer than two non-empty piles, so the game ends. Total: 7 points.

Example 3:

Input: a = 1, b = 8, c = 8
Output: 8
Explanation: One optimal set of moves is to take from the 2nd and 3rd piles for 8 turns until they are empty.
After that, there are fewer than two non-empty piles, so the game ends.

Constraints:

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

移除石子的最大得分。

你正在玩一个单人游戏,面前放置着大小分别为 a​​​​​​、b 和 c​​​​​​ 的 三堆 石子。

每回合你都要从两个 不同的非空堆 中取出一颗石子,并在得分上加 1 分。当存在 两个或更多 的空堆时,游戏停止。

给你三个整数 a 、b 和 c ,返回可以得到的 最大分数 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-score-from-removing-stones
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的思路很容易想到,是贪心,但是做法有好几种。比较容易想到的做法是用 priority queue,但是代码运行起来很慢,这里我提供一个递归的代码,算是次优解吧。

根据题目的规则,每次需要从两个不同的堆中各拿出一个石子。为了让得分尽可能大,我这里采取的方案是优先从两个数量多的堆中取石子,所以我会先对 a,b,c 排序,确保 a > b > c。这样我递归的时候,就优先从 a 和 b 中取石子。

时间 - 应该是石子数量第二多的那一堆的数量,因为只要第二多的那一堆取完了,操作就停止了

空间O(n) - 递归栈空间

Java实现

 1 class Solution {
 2     public int maximumScore(int a, int b, int c) {
 3         // make sure a > b > c
 4         if (a < b) {
 5             return maximumScore(b, a, c);
 6         }
 7         if (b < c) {
 8             return maximumScore(a, c, b);
 9         }
10         return b == 0 ? 0 : 1 + maximumScore(a - 1, b - 1, c);
11     }
12 }

 

LeetCode 题目总结

标签:Stones,3rd,piles,Removing,Maximum,2nd,state,Take,now
From: https://www.cnblogs.com/cnoodle/p/16997527.html

相关文章