Skip to content
Open
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
28 changes: 28 additions & 0 deletions exercises/1000_programs/medium/1874_minimize_product_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def minProductSum(nums1, nums2):
"""
Minimizes the product sum of two arrays of equal length.

Approach:
To minimize the sum, we pair the largest elements of one array with
the smallest elements of the other. This greedy strategy is a
foundational concept in loss optimization and weight adjustment.

Args:
nums1 (list[int]): First array of integers.
nums2 (list[int]): Second array of integers.

Returns:
int: The minimum possible product sum.
"""
# Sorting nums1 in ascending order (smallest to largest)
nums1.sort()

# Sorting nums2 in descending order (largest to smallest)
nums2.sort(reverse=True)

min_sum = 0
# Zip pairs the elements together for multiplication
for n1, n2 in zip(nums1, nums2):
min_sum += n1 * n2

return min_sum