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 de737a5

Browse files
Add Minimum Equal Sum of Two Arrays solution and tests; update README with new problem link
1 parent 4a3b9e4 commit de737a5

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
- https://leetcode.com/problems/product-of-array-except-self/
3939
- Hash Table: https://leetcode.com/problems/sequential-digits/description/
4040
- Hash Table: https://leetcode.com/problems/find-all-duplicates-in-an-array/
41+
- https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/description/?envType=daily-question&envId=2025年05月10日
42+
-
4143

4244

4345
### Topics
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/description/?envType=daily-question&envId=2025年05月10日
2+
3+
void main(List<String> args) {
4+
print(Solution().minSum([3, 2, 0, 1, 0], [6, 5, 0]));
5+
print(Solution().minSum([2, 0, 2, 0], [1, 4]));
6+
}
7+
8+
class Solution {
9+
////! Solved from the first Submission
10+
int minSum(List<int> nums1, List<int> nums2) {
11+
int nums1Total = 0;
12+
int nums2Total = 0;
13+
int nums1Zeros = 0;
14+
int nums2Zeros = 0;
15+
for (var i = 0; i < nums1.length; i++) {
16+
if (nums1[i] == 0) {
17+
nums1Zeros++;
18+
} else {
19+
nums1Total += nums1[i];
20+
}
21+
}
22+
23+
for (var i = 0; i < nums2.length; i++) {
24+
if (nums2[i] == 0) {
25+
nums2Zeros++;
26+
} else {
27+
nums2Total += nums2[i];
28+
}
29+
}
30+
31+
int total1 = nums1Zeros * 1 + nums1Total;
32+
int total2 = nums2Zeros * 1 + nums2Total;
33+
34+
if (nums1Zeros == 0 &&
35+
nums2Zeros == 0 &&
36+
nums1Total > 0 &&
37+
nums2Total > 0 &&
38+
nums1Total != nums2Total) {
39+
return -1;
40+
} else if (nums1Zeros == 0 && nums1Total < total2 ||
41+
nums2Zeros == 0 && nums2Total < total1) {
42+
return -1;
43+
}
44+
45+
return total1 > total2 ? total1 : total2;
46+
}
47+
}

0 commit comments

Comments
(0)

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