LC.P2178[拆分成最多数目的正偶数之和]

方法一:贪心

1
2
3
4
5
6
7
8
9
10
11
12
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;
}
}
  • 时间复杂度:$O(\sqrt {finalSum})$
  • 空间复杂度:$O(1)$