Skip to content

Latest commit

 

History

History
66 lines (46 loc) · 1.42 KB

_1874. Minimize Product Sum of Two Arrays.md

File metadata and controls

66 lines (46 loc) · 1.42 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Array, Greedy, Sorting

Acceptance Rate : 89.47 %


Solutions

C

int compHelper(const void* a, const void* b) {
    return *((int*) a) - *((int*) b);
}

int minProductSum(int* nums1, int nums1Size, int* nums2, int nums2Size){
    int output = 0;

    qsort(nums1, nums1Size, sizeof(int), compHelper);
    qsort(nums2, nums2Size, sizeof(int), compHelper);

    for (int i = 0; i < nums1Size; i++) {
        output += nums1[i] * nums2[nums2Size - i - 1];
    }

    return output;

}

Java

class Solution {
    public int minProductSum(int[] nums1, int[] nums2) {
        int output = 0;

        Arrays.sort(nums1);
        Arrays.sort(nums2);

        for (int i = 0; i < nums1.length; i++) {
            output += nums1[i] * nums2[nums2.length - i - 1];
        }

        return output;
    }
}