Skip to content

Commit 4153136

Browse files
committed
Add a solution to Maximum Split of Positive Even Integers
1 parent 4dd9e34 commit 4153136

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/maximum-split-of-positive-even-integers/
3+
* Primary idea: Greedy. The most split should contain numbers as small as possible.
4+
* Time Complexity: O(logn), Space Complexity: O(1)
5+
*
6+
*/
7+
8+
class MaximumSplitPositiveEvenIntegers {
9+
func maximumEvenSplit(_ finalSum: Int) -> [Int] {
10+
guard finalSum % 2 == 0 else {
11+
return []
12+
}
13+
14+
var res = [Int](), candidate = 2, currentSum = 0
15+
16+
while (currentSum + candidate) <= finalSum {
17+
res.append(candidate)
18+
currentSum += candidate
19+
candidate += 2
20+
}
21+
res[res.count - 1] += finalSum - currentSum
22+
23+
return res
24+
}
25+
}

0 commit comments

Comments
 (0)