Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心 + 排序

由于两个数组都是正整数,要使得乘积和最小,我们可以将两个数组中的最大值和最小值相乘,次大值和次小值相乘,以此类推。

因此,我们将数组 $\textit{nums1}$ 按照升序排序,将数组 $\textit{nums2}$ 按照降序排序,然后将两个数组对应位置的元素相乘,累加即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums1}$ 的长度。

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -95,28 +99,42 @@ class Solution {
class Solution {
public:
int minProductSum(vector<int>& nums1, vector<int>& 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>());
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;
}
};
```

#### 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;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ tags:

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -113,28 +117,42 @@ class Solution {
class Solution {
public:
int minProductSum(vector<int>& nums1, vector<int>& 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>());
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;
}
};
```

#### 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;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class Solution {
public:
int minProductSum(vector<int>& nums1, vector<int>& 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>());
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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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;
}
Loading