Skip to content

Commit 191074f

Browse files
authored
update: 添加问题“2918.数组的最小相等和”的代码和题解(#928)
* archive: multi (#974) Signed-off-by: LetMeFly666 <[email protected]> * words: en (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: Init.Cpp (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: archive.Cpp (#974) 某人穿好了 Signed-off-by: LetMeFly666 <[email protected]> * 2918: WA.Cpp (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: AC.Cpp (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: Init.Py (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: CE.Py (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: AC.Py - AC,64.10%,41.03% (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: AC.Java -AC,100.00%,25.71% (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: WA.go (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: AC.go - AC,17.65%, 29.41% (#974) Signed-off-by: LetMeFly666 <[email protected]> * 2918: init.题解 (#974) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2918.数组的最小相等和”的代码和题解(#928) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent c38de9d commit 191074f

12 files changed

+315
-104
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-10 12:07:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-10 19:00:36
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
public:
15+
long long minSum(vector<int>& nums1, vector<int>& nums2) {
16+
ll s1 = 0, s2 = 0;
17+
int c1 = 0, c2 = 0;
18+
for (int t : nums1) {
19+
s1 += t;
20+
c1 += t == 0;
21+
}
22+
for (int t : nums2) {
23+
s2 += t;
24+
c2 += t == 0;
25+
}
26+
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
27+
return -1;
28+
}
29+
return max(s1 + c1, s2 + c2);
30+
}
31+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-10 12:07:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-10 19:38:55
6+
*/
7+
package main
8+
9+
func cal2918(a []int) (s int64, cnt int64) {
10+
for _, t := range a {
11+
if (t == 0) {
12+
cnt++
13+
} else {
14+
s += int64(t)
15+
}
16+
}
17+
return
18+
}
19+
20+
func minSum(nums1 []int, nums2 []int) int64 {
21+
s1, c1 := cal2918(nums1)
22+
s2, c2 := cal2918(nums2)
23+
if s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0 {
24+
return -1
25+
}
26+
return max(s1 + c1, s2 + c2)
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-10 12:07:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-10 19:32:19
6+
*/
7+
class Solution {
8+
public long minSum(int[] nums1, int[] nums2) {
9+
long s1 = 0, s2 = 0;
10+
int c1 = 0, c2 = 0;
11+
for (int t : nums1) {
12+
s1 += t;
13+
if (t == 0) {
14+
c1++;
15+
}
16+
}
17+
for (int t : nums2) {
18+
s2 += t;
19+
if (t == 0) {
20+
c2++;
21+
}
22+
}
23+
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
24+
return -1;
25+
}
26+
return Math.max(s1 + c1, s2 + c2);
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-10 12:07:54
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-10 19:29:26
6+
'''
7+
from typing import List, Tuple
8+
9+
class Solution:
10+
def cal1(self, nums: List[int]) -> Tuple[int, int]:
11+
s, c = 0, 0
12+
for t in nums:
13+
if t:
14+
s += t
15+
else:
16+
c += 1
17+
return s, c
18+
19+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
20+
s1, c1 = self.cal1(nums1)
21+
s2, c2 = self.cal1(nums2)
22+
if s1 < s2 + c2 and c1 == 0 or s1 + c1 > s2 and c2 == 0:
23+
return -1
24+
return max(s1 + c1, s2 + c2)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@
867867
|2903.找出满足差值条件的下标I|简单|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/25/LeetCode%202903.%E6%89%BE%E5%87%BA%E6%BB%A1%E8%B6%B3%E5%B7%AE%E5%80%BC%E6%9D%A1%E4%BB%B6%E7%9A%84%E4%B8%8B%E6%A0%87I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139195914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/solutions/2789520/letmefly-2903zhao-chu-man-zu-chai-zhi-ti-m92q/" target="_blank">LeetCode题解</a>|
868868
|2908.元素和最小的山形三元组I|简单|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/29/LeetCode%202908.%E5%85%83%E7%B4%A0%E5%92%8C%E6%9C%80%E5%B0%8F%E7%9A%84%E5%B1%B1%E5%BD%A2%E4%B8%89%E5%85%83%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137151595" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/solutions/2714346/letmefly-2908yuan-su-he-zui-xiao-de-shan-h3s9/" target="_blank">LeetCode题解</a>|
869869
|2917.找出数组中的K-or值|简单|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/06/LeetCode%202917.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84K-or%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136497896" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/solutions/2670279/letmefly-2917zhao-chu-shu-zu-zhong-de-k-4wuuh/" target="_blank">LeetCode题解</a>|
870+
|2918.数组的最小相等和|中等|<a href="https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/10/LeetCode%202918.%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%B0%8F%E7%9B%B8%E7%AD%89%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147858203" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/solutions/3673509/letmefly-2918shu-zu-de-zui-xiao-xiang-de-b7ha/" target="_blank">LeetCode题解</a>|
870871
|2923.找到冠军I|简单|<a href="https://leetcode.cn/problems/find-champion-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/12/LeetCode%202923.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137678593" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-i/solutions/2736315/letmefly-2923zhao-dao-guan-jun-ion2he-on-jrxw/" target="_blank">LeetCode题解</a>|
871872
|2924.找到冠军II|中等|<a href="https://leetcode.cn/problems/find-champion-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/13/LeetCode%202924.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137707389" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-ii/solutions/2737408/letmefly-2924zhao-dao-guan-jun-iinao-jin-miks/" target="_blank">LeetCode题解</a>|
872873
|2928.给小朋友们分糖果I|简单|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/01/LeetCode%202928.%E7%BB%99%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E5%88%86%E7%B3%96%E6%9E%9CI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139380754" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/2797856/letmefly-2928gei-xiao-peng-you-men-fen-t-u01r/" target="_blank">LeetCode题解</a>|
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
title: 2918.数组的最小相等和:if-else
3+
date: 2025-05-10 19:39:40
4+
tags: [题解, LeetCode, 中等, 贪心, 数组]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2918.数组的最小相等和:if-else
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/](https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/)
11+
12+
<p>给你两个由正整数和 <code>0</code> 组成的数组 <code>nums1</code> 和 <code>nums2</code> 。</p>
13+
14+
<p>你必须将两个数组中的<strong> 所有</strong> <code>0</code> 替换为 <strong>严格</strong> 正整数,并且满足两个数组中所有元素的和 <strong>相等</strong> 。</p>
15+
16+
<p>返回 <strong>最小</strong> 相等和 ,如果无法使两数组相等,则返回 <code>-1</code><em> </em>。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong class="example">示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>nums1 = [3,2,0,1,0], nums2 = [6,5,0]
24+
<strong>输出:</strong>12
25+
<strong>解释:</strong>可以按下述方式替换数组中的 0 :
26+
- 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。
27+
- 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。
28+
两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等和。
29+
</pre>
30+
31+
<p><strong class="example">示例 2:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>nums1 = [2,0,2,0], nums2 = [1,4]
35+
<strong>输出:</strong>-1
36+
<strong>解释:</strong>无法使两个数组的和相等。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li>
46+
</ul>
47+
48+
## 解题方法:讨论
49+
50+
假设$nums1$的和为$s1$且$nums1$中有$c1$个$0$,$nums2$的和以及零的个数分别为$s2$和$c2$,则有:
51+
52+
+ 若$s1 < s2 + c2 and c1 == 0$,说明$nums2$的和至少变为$s2+c2$,比$s1$大并且$s1$中没有$0$可以使其和变大,直接返回$-1$;
53+
+ 若$s1 + c1 > s2 and cs == 0$,同理,直接返回$-1$;
54+
+ 否则,返回$max(s1 + c1, s2 + c2)$。
55+
56+
时空复杂度分析:
57+
58+
+ 时间复杂度$O(len(nums1) + len(nums2))$
59+
+ 空间复杂度$O(1)$
60+
61+
### AC代码
62+
63+
#### C++
64+
65+
```cpp
66+
/*
67+
* @Author: LetMeFly
68+
* @Date: 2025-05-10 12:07:54
69+
* @LastEditors: LetMeFly.xyz
70+
* @LastEditTime: 2025-05-10 19:00:36
71+
*/
72+
typedef long long ll;
73+
74+
class Solution {
75+
public:
76+
long long minSum(vector<int>& nums1, vector<int>& nums2) {
77+
ll s1 = 0, s2 = 0;
78+
int c1 = 0, c2 = 0;
79+
for (int t : nums1) {
80+
s1 += t;
81+
c1 += t == 0;
82+
}
83+
for (int t : nums2) {
84+
s2 += t;
85+
c2 += t == 0;
86+
}
87+
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
88+
return -1;
89+
}
90+
return max(s1 + c1, s2 + c2);
91+
}
92+
};
93+
```
94+
95+
#### Python
96+
97+
```python
98+
'''
99+
Author: LetMeFly
100+
Date: 2025-05-10 12:07:54
101+
LastEditors: LetMeFly.xyz
102+
LastEditTime: 2025-05-10 19:29:26
103+
'''
104+
from typing import List, Tuple
105+
106+
class Solution:
107+
def cal1(self, nums: List[int]) -> Tuple[int, int]:
108+
s, c = 0, 0
109+
for t in nums:
110+
if t:
111+
s += t
112+
else:
113+
c += 1
114+
return s, c
115+
116+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
117+
s1, c1 = self.cal1(nums1)
118+
s2, c2 = self.cal1(nums2)
119+
if s1 < s2 + c2 and c1 == 0 or s1 + c1 > s2 and c2 == 0:
120+
return -1
121+
return max(s1 + c1, s2 + c2)
122+
```
123+
124+
#### Java
125+
126+
```java
127+
/*
128+
* @Author: LetMeFly
129+
* @Date: 2025-05-10 12:07:54
130+
* @LastEditors: LetMeFly.xyz
131+
* @LastEditTime: 2025-05-10 19:32:19
132+
*/
133+
class Solution {
134+
public long minSum(int[] nums1, int[] nums2) {
135+
long s1 = 0, s2 = 0;
136+
int c1 = 0, c2 = 0;
137+
for (int t : nums1) {
138+
s1 += t;
139+
if (t == 0) {
140+
c1++;
141+
}
142+
}
143+
for (int t : nums2) {
144+
s2 += t;
145+
if (t == 0) {
146+
c2++;
147+
}
148+
}
149+
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
150+
return -1;
151+
}
152+
return Math.max(s1 + c1, s2 + c2);
153+
}
154+
}
155+
```
156+
157+
#### Go
158+
159+
```go
160+
/*
161+
* @Author: LetMeFly
162+
* @Date: 2025-05-10 12:07:54
163+
* @LastEditors: LetMeFly.xyz
164+
* @LastEditTime: 2025-05-10 19:38:55
165+
*/
166+
package main
167+
168+
func cal2918(a []int) (s int64, cnt int64) {
169+
for _, t := range a {
170+
if (t == 0) {
171+
cnt++
172+
} else {
173+
s += int64(t)
174+
}
175+
}
176+
return
177+
}
178+
179+
func minSum(nums1 []int, nums2 []int) int64 {
180+
s1, c1 := cal2918(nums1)
181+
s2, c2 := cal2918(nums2)
182+
if s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0 {
183+
return -1
184+
}
185+
return max(s1 + c1, s2 + c2)
186+
}
187+
```
188+
189+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147858203)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/10/LeetCode%202918.%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%B0%8F%E7%9B%B8%E7%AD%89%E5%92%8C/)哦~
190+
>
191+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ git remote prune origin
269269

270270
执行起来[嘎嘎爽](https://github.com/LetMeFly666/LeetCode/blob/dc2fd35e116bddf5d1e0cbbadba5ce868e4880bb/tryGoPy/git%20remote%20prune)
271271

272+
### git status等文件名显示中文
273+
274+
```bash
275+
git config --global core.quotepath false
276+
```
277+
272278
## About HTML
273279

274280
### 空白字符

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ categories: [自用]
788788
|sportsmanship|n. 运动员风范,体育精神|
789789
|abasement|n. 身份低微,屈尊,降低|
790790
|||
791-
|swan|n. 天鹅<br/>v. 悠游,悠然闲逛|
791+
|<font color="#28bea0" title="二次复习">swan</font>|n. 天鹅<br/>v. 悠游,悠然闲逛|
792792
|confide|v. (向某人)吐露(秘密/隐私)|
793793
|analogue|adj. 模拟的,指针式的<br/>n. 相似物,类似事情|
794794
|kangaroo|n. 袋鼠|
@@ -1136,6 +1136,12 @@ categories: [自用]
11361136
|humane|ajd. 善良的,仁慈的,人道的|
11371137
|||
11381138
|ultimo|adj. 上个月的<br/>adv. 上个月地|
1139+
|||
1140+
|hysteria|n. 情绪失控,歇斯底里,癔症,大肆鼓吹|
1141+
|||
1142+
|deficient|adj. 缺乏的,不足的,有缺点的,有缺陷的|
1143+
|||
1144+
|barometer|n. 气压计,晴雨表|
11391145

11401146
<p class="wordCounts">单词收录总数</p>
11411147

filterSpacePoint.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)