替换 0 后两个数组的最小相等和
Minimum Equal Sum of Two Arrays After Replacing Zeros
题目详情
问题:替换 0 后两个数组的最小相等和
考察:数组、贪心
来源:DSA Prep / Citadel
链接:https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros
英文原题
Problem: Minimum Equal Sum of Two Arrays After Replacing Zeros
Patterns: Array, Greedy
Recency: 2yr
Link: https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros
Source: https://www.dsaprep.dev/blog/citadel-coding-interview-questions/
解析
思路:每个 0 至少替换成 1,因此先计算两个数组的最小可能和。若两个数组都有 0,则答案是两个最小和的较大者;若只有一边有 0,需要判断它能否补到另一边;都没有 0 时必须原和相等。
复杂度:时间 O(n+m),空间 O(1)。
英文解析
Approach: Replace every zero by at least `1` and compute each array’s minimum possible sum. If both arrays contain zeros, the answer is the larger minimum sum; if only one side has zeros, it must be able to increase up to the fixed sum of the other side; if neither has zeros, the original sums must already match.
Complexity: Time , space .