From 092111f36877742c209da72306eca1657d0501d4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 2 Oct 2024 08:05:49 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1874 --- .../README.md | 56 ++++++++++++------- .../README_EN.md | 56 ++++++++++++------- .../Solution.cpp | 13 +++-- .../Solution.go | 11 ++-- .../Solution.java | 9 +-- .../Solution.py | 7 +-- .../Solution.ts | 9 +++ 7 files changed, 102 insertions(+), 59 deletions(-) create mode 100644 solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.ts diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README.md b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README.md index 59c561eb80207..3c937e5491b2b 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README.md +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README.md @@ -56,7 +56,13 @@ tags: -### 方法一 +### 方法一:贪心 + 排序 + +由于两个数组都是正整数,要使得乘积和最小,我们可以将两个数组中的最大值和最小值相乘,次大值和次小值相乘,以此类推。 + +因此,我们将数组 $\textit{nums1}$ 按照升序排序,将数组 $\textit{nums2}$ 按照降序排序,然后将两个数组对应位置的元素相乘,累加即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums1}$ 的长度。 @@ -66,11 +72,8 @@ tags: class Solution: def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: nums1.sort() - nums2.sort() - n, res = len(nums1), 0 - for i in range(n): - res += nums1[i] * nums2[n - i - 1] - return res + nums2.sort(reverse=True) + return sum(x * y for x, y in zip(nums1, nums2)) ``` #### Java @@ -80,11 +83,12 @@ class Solution { public int minProductSum(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); - int n = nums1.length, res = 0; + int n = nums1.length; + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[n - i - 1]; } - return res; + return ans; } } ``` @@ -95,13 +99,14 @@ class Solution { class Solution { public: int minProductSum(vector& nums1, vector& nums2) { - sort(nums1.begin(), nums1.end()); - sort(nums2.begin(), nums2.end()); - int n = nums1.size(), res = 0; + ranges::sort(nums1); + ranges::sort(nums2, greater()); + int n = nums1.size(); + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[i]; } - return res; + return ans; } }; ``` @@ -109,14 +114,27 @@ public: #### Go ```go -func minProductSum(nums1 []int, nums2 []int) int { +func minProductSum(nums1 []int, nums2 []int) (ans int) { sort.Ints(nums1) sort.Ints(nums2) - res, n := 0, len(nums1) - for i, num := range nums1 { - res += num * nums2[n-i-1] + for i, x := range nums1 { + ans += x * nums2[len(nums2)-1-i] } - return res + return +} +``` + +#### TypeScript + +```ts +function minProductSum(nums1: number[], nums2: number[]): number { + nums1.sort((a, b) => a - b); + nums2.sort((a, b) => b - a); + let ans = 0; + for (let i = 0; i < nums1.length; ++i) { + ans += nums1[i] * nums2[i]; + } + return ans; } ``` diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md index 36c4c2c85b10b..f4db46805a1ca 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md @@ -74,7 +74,13 @@ tags: -### Solution 1 +### Solution 1: Greedy + Sorting + +Since both arrays consist of positive integers, to minimize the sum of products, we can multiply the largest value in one array with the smallest value in the other array, the second largest with the second smallest, and so on. + +Therefore, we sort the array $\textit{nums1}$ in ascending order and the array $\textit{nums2}$ in descending order. Then, we multiply the corresponding elements of the two arrays and sum the results. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums1}$. @@ -84,11 +90,8 @@ tags: class Solution: def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: nums1.sort() - nums2.sort() - n, res = len(nums1), 0 - for i in range(n): - res += nums1[i] * nums2[n - i - 1] - return res + nums2.sort(reverse=True) + return sum(x * y for x, y in zip(nums1, nums2)) ``` #### Java @@ -98,11 +101,12 @@ class Solution { public int minProductSum(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); - int n = nums1.length, res = 0; + int n = nums1.length; + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[n - i - 1]; } - return res; + return ans; } } ``` @@ -113,13 +117,14 @@ class Solution { class Solution { public: int minProductSum(vector& nums1, vector& nums2) { - sort(nums1.begin(), nums1.end()); - sort(nums2.begin(), nums2.end()); - int n = nums1.size(), res = 0; + ranges::sort(nums1); + ranges::sort(nums2, greater()); + int n = nums1.size(); + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[i]; } - return res; + return ans; } }; ``` @@ -127,14 +132,27 @@ public: #### Go ```go -func minProductSum(nums1 []int, nums2 []int) int { +func minProductSum(nums1 []int, nums2 []int) (ans int) { sort.Ints(nums1) sort.Ints(nums2) - res, n := 0, len(nums1) - for i, num := range nums1 { - res += num * nums2[n-i-1] + for i, x := range nums1 { + ans += x * nums2[len(nums2)-1-i] } - return res + return +} +``` + +#### TypeScript + +```ts +function minProductSum(nums1: number[], nums2: number[]): number { + nums1.sort((a, b) => a - b); + nums2.sort((a, b) => b - a); + let ans = 0; + for (let i = 0; i < nums1.length; ++i) { + ans += nums1[i] * nums2[i]; + } + return ans; } ``` diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.cpp b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.cpp index cd73624cc6f3e..9447291f72f86 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.cpp +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.cpp @@ -1,12 +1,13 @@ class Solution { public: int minProductSum(vector& nums1, vector& nums2) { - sort(nums1.begin(), nums1.end()); - sort(nums2.begin(), nums2.end()); - int n = nums1.size(), res = 0; + ranges::sort(nums1); + ranges::sort(nums2, greater()); + int n = nums1.size(); + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[i]; } - return res; + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.go b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.go index a9ffeb85ff95c..80ccfed37a8d1 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.go +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.go @@ -1,9 +1,8 @@ -func minProductSum(nums1 []int, nums2 []int) int { +func minProductSum(nums1 []int, nums2 []int) (ans int) { sort.Ints(nums1) sort.Ints(nums2) - res, n := 0, len(nums1) - for i, num := range nums1 { - res += num * nums2[n-i-1] + for i, x := range nums1 { + ans += x * nums2[len(nums2)-1-i] } - return res -} \ No newline at end of file + return +} diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.java b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.java index 376815b101e24..9c8ba6ae90a6e 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.java +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.java @@ -2,10 +2,11 @@ class Solution { public int minProductSum(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); - int n = nums1.length, res = 0; + int n = nums1.length; + int ans = 0; for (int i = 0; i < n; ++i) { - res += nums1[i] * nums2[n - i - 1]; + ans += nums1[i] * nums2[n - i - 1]; } - return res; + return ans; } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.py b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.py index 1d99ea583883e..07fe975137888 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.py +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.py @@ -1,8 +1,5 @@ class Solution: def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: nums1.sort() - nums2.sort() - n, res = len(nums1), 0 - for i in range(n): - res += nums1[i] * nums2[n - i - 1] - return res + nums2.sort(reverse=True) + return sum(x * y for x, y in zip(nums1, nums2)) diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.ts b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.ts new file mode 100644 index 0000000000000..cec5057122cd5 --- /dev/null +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/Solution.ts @@ -0,0 +1,9 @@ +function minProductSum(nums1: number[], nums2: number[]): number { + nums1.sort((a, b) => a - b); + nums2.sort((a, b) => b - a); + let ans = 0; + for (let i = 0; i < nums1.length; ++i) { + ans += nums1[i] * nums2[i]; + } + return ans; +}