Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e68736a

Browse files
Add solution for Maximum Split of Positive Even Integers
1 parent 47515b9 commit e68736a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package solutions;
2+
3+
import java.util.*;
4+
5+
// [Problem] https://leetcode.com/problems/maximum-split-of-positive-even-integers
6+
class MaximumSplitOfIntegers {
7+
// Min heap
8+
// O(nlogn) time, O(n) space
9+
// where n = total number of meetings in schedule
10+
public List<Long> maximumEvenSplit(long finalSum) {
11+
if (finalSum % 2 == 1) {
12+
return new ArrayList<>();
13+
}
14+
List<Long> numbers = new ArrayList<>();
15+
long currentNumber = 2L;
16+
while (finalSum > 0) {
17+
if (finalSum - currentNumber > currentNumber) {
18+
finalSum -= currentNumber;
19+
numbers.add(currentNumber);
20+
} else {
21+
numbers.add(finalSum);
22+
break;
23+
}
24+
currentNumber += 2;
25+
}
26+
return numbers;
27+
}
28+
29+
// Test
30+
public static void main(String[] args) {
31+
MaximumSplitOfIntegers solution = new MaximumSplitOfIntegers();
32+
33+
long input1 = 12;
34+
List<Long> expectedOutput1 = List.of(2L, 4L, 6L);
35+
List<Long> actualOutput1 = solution.maximumEvenSplit(input1);
36+
System.out.println("Test 1 passed? " + expectedOutput1.equals(actualOutput1));
37+
38+
long input2 = 28;
39+
List<Long> expectedOutput2 = List.of(2L, 4L, 6L, 16L);
40+
List<Long> actualOutput2 = solution.maximumEvenSplit(input2);
41+
System.out.println("Test 2 passed? " + expectedOutput2.equals(actualOutput2));
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /