You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md
+37-6
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ Thus, we return an empty array.
53
53
<pre>
54
54
<strong>Input:</strong> finalSum = 28
55
55
<strong>Output:</strong> [6,8,2,12]
56
-
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>.
56
+
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>.
57
57
<code>(6 + 8 + 2 + 12)</code> has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
58
58
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
59
59
</pre>
@@ -71,7 +71,13 @@ Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
71
71
72
72
<!-- solution:start -->
73
73
74
-
### Solution 1
74
+
### Solution 1: Greedy
75
+
76
+
If $\textit{finalSum}$ is odd, it cannot be split into the sum of several distinct positive even integers, so we directly return an empty array.
77
+
78
+
Otherwise, we can greedily split $\textit{finalSum}$ in the order of $2, 4, 6, \cdots$, until $\textit{finalSum}$ can no longer be split into a different positive even integer. At this point, we add the remaining $\textit{finalSum}$ to the last positive even integer.
79
+
80
+
The time complexity is $O(\sqrt{\textit{finalSum}})$, and ignoring the space consumption of the answer array, the space complexity is $O(1)$.
75
81
76
82
<!-- tabs:start -->
77
83
@@ -80,13 +86,13 @@ Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
0 commit comments