返回介绍

solution / 1700-1799 / 1753.Maximum Score From Removing Stones / README_EN

发布于 2024-06-17 01:03:14 字数 5088 浏览 0 评论 0 收藏 0

1753. Maximum Score From Removing Stones

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def maximumScore(self, a: int, b: int, c: int) -> int:
    s = sorted([a, b, c])
    ans = 0
    while s[1]:
      ans += 1
      s[1] -= 1
      s[2] -= 1
      s.sort()
    return ans
class Solution {
  public int maximumScore(int a, int b, int c) {
    int[] s = new int[] {a, b, c};
    Arrays.sort(s);
    int ans = 0;
    while (s[1] > 0) {
      ++ans;
      s[1]--;
      s[2]--;
      Arrays.sort(s);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumScore(int a, int b, int c) {
    vector<int> s = {a, b, c};
    sort(s.begin(), s.end());
    int ans = 0;
    while (s[1]) {
      ++ans;
      s[1]--;
      s[2]--;
      sort(s.begin(), s.end());
    }
    return ans;
  }
};
func maximumScore(a int, b int, c int) (ans int) {
  s := []int{a, b, c}
  sort.Ints(s)
  for s[1] > 0 {
    ans++
    s[1]--
    s[2]--
    sort.Ints(s)
  }
  return
}

Solution 2

class Solution:
  def maximumScore(self, a: int, b: int, c: int) -> int:
    a, b, c = sorted([a, b, c])
    if a + b < c:
      return a + b
    return (a + b + c) >> 1
class Solution {
  public int maximumScore(int a, int b, int c) {
    int[] s = new int[] {a, b, c};
    Arrays.sort(s);
    if (s[0] + s[1] < s[2]) {
      return s[0] + s[1];
    }
    return (a + b + c) >> 1;
  }
}
class Solution {
public:
  int maximumScore(int a, int b, int c) {
    vector<int> s = {a, b, c};
    sort(s.begin(), s.end());
    if (s[0] + s[1] < s[2]) return s[0] + s[1];
    return (a + b + c) >> 1;
  }
};
func maximumScore(a int, b int, c int) int {
  s := []int{a, b, c}
  sort.Ints(s)
  if s[0]+s[1] < s[2] {
    return s[0] + s[1]
  }
  return (a + b + c) >> 1
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文