fix:拆分成最多数目的正偶数之和

上级 705a82cf
"""
K 件物品的最大和
"""
from typing import List
class Solution:
......
"""
K 件物品的最大和
"""
from typing import List
class Solution:
......
"""
拆分成最多数目的正偶数之和
"""
from typing import List
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
"""
怎么样遍历,只需要遍历一次呢?贪心算法 i不断加 finalSum不断减
:param finalSum:
:return:
"""
if finalSum % 2 == 1:
return []
res = []
i = 2
while i <= finalSum:
res.append(i)
finalSum -= i
i += 2
# 当i的值大于finalSum时无法继续遍历,需要把剩余的数加到最后一位上
res[-1] += finalSum
return res
if __name__ == '__main__':
result = Solution().maximumEvenSplit(28)
print(result)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册