返回介绍

solution / 2100-2199 / 2178.Maximum Split of Positive Even Integers / README_EN

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

2178. Maximum Split of Positive Even Integers

中文文档

Description

You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.

  • For example, given finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (12), (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the maximum number of integers. Note that finalSum cannot be split into (2 + 2 + 4 + 4) as all the numbers should be unique.

Return _a list of integers that represent a valid split containing a maximum number of integers_. If no valid split exists for finalSum, return _an empty list_. You may return the integers in any order.

 

Example 1:

Input: finalSum = 12
Output: [2,4,6]
Explanation: The following are valid splits: (12), (2 + 10), (2 + 4 + 6), and (4 + 8).
(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].
Note that [2,6,4], [6,2,4], etc. are also accepted.

Example 2:

Input: finalSum = 7
Output: []
Explanation: There are no valid splits for the given finalSum.
Thus, we return an empty array.

Example 3:

Input: finalSum = 28
Output: [6,8,2,12]
Explanation: The following are valid splits: (2 + 26), (6 + 8 + 2 + 12), and (4 + 24). 
(6 + 8 + 2 + 12) has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.

 

Constraints:

  • 1 <= finalSum <= 1010

Solutions

Solution 1

class Solution:
  def maximumEvenSplit(self, finalSum: int) -> List[int]:
    if finalSum % 2:
      return []
    i = 2
    ans = []
    while i <= finalSum:
      ans.append(i)
      finalSum -= i
      i += 2
    ans[-1] += finalSum
    return ans
class Solution {
  public List<Long> maximumEvenSplit(long finalSum) {
    List<Long> ans = new ArrayList<>();
    if (finalSum % 2 == 1) {
      return ans;
    }
    for (long i = 2; i <= finalSum; i += 2) {
      ans.add(i);
      finalSum -= i;
    }
    ans.add(ans.remove(ans.size() - 1) + finalSum);
    return ans;
  }
}
class Solution {
public:
  vector<long long> maximumEvenSplit(long long finalSum) {
    vector<long long> ans;
    if (finalSum % 2) return ans;
    for (long long i = 2; i <= finalSum; i += 2) {
      ans.push_back(i);
      finalSum -= i;
    }
    ans.back() += finalSum;
    return ans;
  }
};
func maximumEvenSplit(finalSum int64) (ans []int64) {
  if finalSum%2 == 1 {
    return
  }
  for i := int64(2); i <= finalSum; i += 2 {
    ans = append(ans, i)
    finalSum -= i
  }
  ans[len(ans)-1] += finalSum
  return
}
function maximumEvenSplit(finalSum: number): number[] {
  const ans: number[] = [];
  if (finalSum % 2 === 1) {
    return ans;
  }
  for (let i = 2; i <= finalSum; i += 2) {
    ans.push(i);
    finalSum -= i;
  }
  ans[ans.length - 1] += finalSum;
  return ans;
}
public class Solution {
  public IList<long> MaximumEvenSplit(long finalSum) {
    IList<long> ans = new List<long>();
    if (finalSum % 2 == 1) {
      return ans;
    }
    for (long i = 2; i <= finalSum; i += 2) {
      ans.Add(i);
      finalSum -= i;
    }
    ans[ans.Count - 1] += finalSum;
    return ans;
  }
}

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

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

发布评论

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