Skip to content

Commit

Permalink
添加了问题“2578. 最小和分割”的代码和题解
Browse files Browse the repository at this point in the history
  • Loading branch information
LetMeFly666 committed Oct 9, 2023
1 parent f1b4978 commit 67c65cc
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Codes/2578-split-with-minimum-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* @Author: LetMeFly
* @Date: 2023-10-09 09:06:16
* @LastEditors: LetMeFly
* @LastEditTime: 2023-10-09 09:09:44
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

class Solution {
public:
int splitNum(int num) {
string s = to_string(num);
sort(s.begin(), s.end());
string n1, n2;
for (int i = 0; i < s.size(); i++) {
(i % 2 ? n2 : n1) += s[i];
}
// cout << "n1: " << n1 << ", n2: " << n2 << endl; //**********
return atoi(n1.c_str()) + atoi(n2.c_str());
}
};
13 changes: 13 additions & 0 deletions Codes/2578-split-with-minimum-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'''
Author: LetMeFly
Date: 2023-10-09 09:10:33
LastEditors: LetMeFly
LastEditTime: 2023-10-09 09:12:51
'''
class Solution:
def splitNum(self, num: int) -> int:
s = sorted(str(num))
n = ['', '']
for i in range(len(s)):
n[i % 2] += s[i]
return int(n[0]) + int(n[1])
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ int main() {
|2511.最多可以摧毁的敌人城堡数目|简单|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/02/LeetCode%202511.%E6%9C%80%E5%A4%9A%E5%8F%AF%E4%BB%A5%E6%91%A7%E6%AF%81%E7%9A%84%E6%95%8C%E4%BA%BA%E5%9F%8E%E5%A0%A1%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132634912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/2422392/letmefly-2511zui-duo-ke-yi-cui-hui-de-di-r7kt/" target="_blank">LeetCode题解</a>|
|2544.交替数字和|简单|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/12/LeetCode%202544.%E4%BA%A4%E6%9B%BF%E6%95%B0%E5%AD%97%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131673485" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/2340450/letmefly-2544jiao-ti-shu-zi-he-by-tisfy-aa0f/" target="_blank">LeetCode题解</a>|
|2559.统计范围内的元音字符串数|中等|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/06/02/LeetCode%202559.%E7%BB%9F%E8%AE%A1%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E5%85%83%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131014779" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/2294361/letmefly-2559tong-ji-fan-wei-nei-de-yuan-mq4f/" target="_blank">LeetCode题解</a>|
|2578.最小和分割|简单|<a href="https://leetcode.cn/problems/split-with-minimum-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/09/LeetCode%202578.%E6%9C%80%E5%B0%8F%E5%92%8C%E5%88%86%E5%89%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133694187" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-with-minimum-sum/solutions/2473429/letmefly-2578zui-xiao-he-fen-ge-tan-xin-pxbyl/" target="_blank">LeetCode题解</a>|
|2582.递枕头|简单|<a href="https://leetcode.cn/problems/pass-the-pillow/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/26/LeetCode%202582.%E9%80%92%E6%9E%95%E5%A4%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133294825" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pass-the-pillow/solutions/2459194/letmefly-2582di-zhen-tou-qing-xi-de-hua-ienu5/" target="_blank">LeetCode题解</a>|
|2591.将钱分给最多的儿童|简单|<a href="https://leetcode.cn/problems/distribute-money-to-maximum-children/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/22/LeetCode%202591.%E5%B0%86%E9%92%B1%E5%88%86%E7%BB%99%E6%9C%80%E5%A4%9A%E7%9A%84%E5%84%BF%E7%AB%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133167460" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-money-to-maximum-children/solutions/2454112/letmefly-2591jiang-qian-fen-gei-zui-duo-2k1tq/" target="_blank">LeetCode题解</a>|
|2596.检查骑士巡视方案|中等|<a href="https://leetcode.cn/problems/check-knight-tour-configuration/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/13/LeetCode%202596.%E6%A3%80%E6%9F%A5%E9%AA%91%E5%A3%AB%E5%B7%A1%E8%A7%86%E6%96%B9%E6%A1%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132847346" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-knight-tour-configuration/solutions/2440047/letmefly-2596jian-cha-qi-shi-xun-shi-fan-2q5j/" target="_blank">LeetCode题解</a>|
Expand Down
108 changes: 108 additions & 0 deletions Solutions/LeetCode 2578.最小和分割.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: 2578.最小和分割
date: 2023-10-09 09:13:42
tags: [题解, LeetCode, 简单, 贪心, 数学, 排序]
---

# 【LetMeFly】2578.最小和分割:贪心(数学)

力扣题目链接:[https://leetcode.cn/problems/split-with-minimum-sum/](https://leetcode.cn/problems/split-with-minimum-sum/)

<p>给你一个正整数&nbsp;<code>num</code>&nbsp;,请你将它分割成两个非负整数&nbsp;<code>num1</code> 和&nbsp;<code>num2</code>&nbsp;,满足:</p>

<ul>
<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;直接连起来,得到&nbsp;<code>num</code>&nbsp;各数位的一个排列。

<ul>
<li>换句话说,<code>num1</code> 和&nbsp;<code>num2</code>&nbsp;中所有数字出现的次数之和等于&nbsp;<code>num</code>&nbsp;中所有数字出现的次数。</li>
</ul>
</li>
<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;可以包含前导 0 。</li>
</ul>

<p>请你返回&nbsp;<code>num1</code> 和 <code>num2</code>&nbsp;可以得到的和的 <strong>最小</strong> 值。</p>

<p><strong>注意:</strong></p>

<ul>
<li><code>num</code>&nbsp;保证没有前导 0 。</li>
<li><code>num1</code> 和&nbsp;<code>num2</code>&nbsp;中数位顺序可以与&nbsp;<code>num</code>&nbsp;中数位顺序不同。</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<b>输入:</b>num = 4325
<b>输出:</b>59
<b>解释:</b>我们可以将 4325 分割成 <code>num1 </code>= 24 和 num2<code> = </code>35 ,和为 59 ,59 是最小和。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<b>输入:</b>num = 687
<b>输出:</b>75
<b>解释:</b>我们可以将 687 分割成 <code>num1</code> = 68 和 <code>num2 </code>= 7 ,和为最优值 75 。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>10 &lt;= num &lt;= 10<sup>9</sup></code></li>
</ul>



## 方法一:贪心(数学)

先说结论:将给定数字转为字符串后将其中字符从小到大排序,然后依次分配给两个新数字即可。

不严谨的原理描述:

+ 越高位数字尽量越小,因此要从小到大排序
+ 最终返回的是两数之和,所以首先位数越小越好,因此尽可能两个数字长度相等
+ 若两个数长度不相等,更长的那个数字的最高位要尽可能小(例如将```23456```分成```246``````35```,唯一的百位是最小的```2```

结论中描述的分法恰好满足。

+ 时间复杂度$O(k\log k)$,其中$k = \log num$
+ 空间复杂度$O(\log k)$

### AC代码

#### C++

```cpp
class Solution {
public:
int splitNum(int num) {
string s = to_string(num);
sort(s.begin(), s.end());
string n1, n2;
for (int i = 0; i < s.size(); i++) {
(i % 2 ? n2 : n1) += s[i];
}
// cout << "n1: " << n1 << ", n2: " << n2 << endl; //**********
return atoi(n1.c_str()) + atoi(n2.c_str());
}
};
```
#### Python
```python
class Solution:
def splitNum(self, num: int) -> int:
s = sorted(str(num))
n = ['', '']
for i in range(len(s)):
n[i % 2] += s[i]
return int(n[0]) + int(n[1])
```

> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2023/10/09/LeetCode%202578.%E6%9C%80%E5%B0%8F%E5%92%8C%E5%88%86%E5%89%B2/)哦~
> Tisfy:[https://letmefly.blog.csdn.net/article/details/133694187](https://letmefly.blog.csdn.net/article/details/133694187)

0 comments on commit 67c65cc

Please sign in to comment.