File tree Expand file tree Collapse file tree
exercises/1000_programs/medium Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ def minProductSum (nums1 , nums2 ):
2+ """
3+ Minimizes the product sum of two arrays of equal length.
4+
5+ Approach:
6+ To minimize the sum, we pair the largest elements of one array with
7+ the smallest elements of the other. This greedy strategy is a
8+ foundational concept in loss optimization and weight adjustment.
9+
10+ Args:
11+ nums1 (list[int]): First array of integers.
12+ nums2 (list[int]): Second array of integers.
13+
14+ Returns:
15+ int: The minimum possible product sum.
16+ """
17+ # Sorting nums1 in ascending order (smallest to largest)
18+ nums1 .sort ()
19+
20+ # Sorting nums2 in descending order (largest to smallest)
21+ nums2 .sort (reverse = True )
22+
23+ min_sum = 0
24+ # Zip pairs the elements together for multiplication
25+ for n1 , n2 in zip (nums1 , nums2 ):
26+ min_sum += n1 * n2
27+
28+ return min_sum
You can’t perform that action at this time.
0 commit comments