Skip to content

feat: add solutions to lc problem: No.2178 #3986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ tags:

### 方法一:贪心

如果 $finalSum$ 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。
如果 $\textit{finalSum}$ 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。

否则,我们可以贪心地按照 $2, 4, 6, \cdots$ 的顺序拆分 $finalSum$,直到 $finalSum$ 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 $finalSum$ 加到最后一个正偶数上即可。
否则,我们可以贪心地按照 $2, 4, 6, \cdots$ 的顺序拆分 $\textit{finalSum}$,直到 $\textit{finalSum}$ 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 $\textit{finalSum}$ 加到最后一个正偶数上即可。

时间复杂度 $O(\sqrt{finalSum})$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。
时间复杂度 $O(\sqrt{\textit{finalSum}})$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -88,13 +88,13 @@ tags:
```python
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
if finalSum % 2:
if finalSum & 1:
return []
i = 2
ans = []
i = 2
while i <= finalSum:
ans.append(i)
finalSum -= i
ans.append(i)
i += 2
ans[-1] += finalSum
return ans
Expand Down Expand Up @@ -126,7 +126,9 @@ class Solution {
public:
vector<long long> maximumEvenSplit(long long finalSum) {
vector<long long> ans;
if (finalSum % 2) return ans;
if (finalSum % 2) {
return ans;
}
for (long long i = 2; i <= finalSum; i += 2) {
ans.push_back(i);
finalSum -= i;
Expand Down Expand Up @@ -170,6 +172,29 @@ function maximumEvenSplit(finalSum: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_even_split(mut final_sum: i64) -> Vec<i64> {
let mut ans = Vec::new();
if final_sum % 2 != 0 {
return ans;
}
let mut i = 2;
while i <= final_sum {
ans.push(i);
final_sum -= i;
i += 2;
}
if let Some(last) = ans.last_mut() {
*last += final_sum;
}
ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Thus, we return an empty array.
<pre>
<strong>Input:</strong> finalSum = 28
<strong>Output:</strong> [6,8,2,12]
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>.
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>.
<code>(6 + 8 + 2 + 12)</code> has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
</pre>
Expand All @@ -71,7 +71,13 @@ Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

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.

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.

The time complexity is $O(\sqrt{\textit{finalSum}})$, and ignoring the space consumption of the answer array, the space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -80,13 +86,13 @@ Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
```python
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
if finalSum % 2:
if finalSum & 1:
return []
i = 2
ans = []
i = 2
while i <= finalSum:
ans.append(i)
finalSum -= i
ans.append(i)
i += 2
ans[-1] += finalSum
return ans
Expand Down Expand Up @@ -118,7 +124,9 @@ class Solution {
public:
vector<long long> maximumEvenSplit(long long finalSum) {
vector<long long> ans;
if (finalSum % 2) return ans;
if (finalSum % 2) {
return ans;
}
for (long long i = 2; i <= finalSum; i += 2) {
ans.push_back(i);
finalSum -= i;
Expand Down Expand Up @@ -162,6 +170,29 @@ function maximumEvenSplit(finalSum: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_even_split(mut final_sum: i64) -> Vec<i64> {
let mut ans = Vec::new();
if final_sum % 2 != 0 {
return ans;
}
let mut i = 2;
while i <= final_sum {
ans.push(i);
final_sum -= i;
i += 2;
}
if let Some(last) = ans.last_mut() {
*last += final_sum;
}
ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ class Solution {
public:
vector<long long> maximumEvenSplit(long long finalSum) {
vector<long long> ans;
if (finalSum % 2) return ans;
if (finalSum % 2) {
return ans;
}
for (long long i = 2; i <= finalSum; i += 2) {
ans.push_back(i);
finalSum -= i;
}
ans.back() += finalSum;
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Solution:
def maximumEvenSplit(self, finalSum: int) -> List[int]:
if finalSum % 2:
if finalSum & 1:
return []
i = 2
ans = []
i = 2
while i <= finalSum:
ans.append(i)
finalSum -= i
ans.append(i)
i += 2
ans[-1] += finalSum
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
impl Solution {
pub fn maximum_even_split(mut final_sum: i64) -> Vec<i64> {
let mut ans = Vec::new();
if final_sum % 2 != 0 {
return ans;
}
let mut i = 2;
while i <= final_sum {
ans.push(i);
final_sum -= i;
i += 2;
}
if let Some(last) = ans.last_mut() {
*last += final_sum;
}
ans
}
}
Loading