From 6f92f758466793a9b463415cedde5905b3967952 Mon Sep 17 00:00:00 2001 From: Osamsami <145556347+Osamsami@users.noreply.github.com> Date: Tue, 31 Mar 2026 16:12:33 -0800 Subject: [PATCH] feat: implement greedy optimization for minimize product sum #1874 --- .../medium/1874_minimize_product_sum.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 exercises/1000_programs/medium/1874_minimize_product_sum.py diff --git a/exercises/1000_programs/medium/1874_minimize_product_sum.py b/exercises/1000_programs/medium/1874_minimize_product_sum.py new file mode 100644 index 0000000..8f7e0a9 --- /dev/null +++ b/exercises/1000_programs/medium/1874_minimize_product_sum.py @@ -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 \ No newline at end of file