Skip to content

Commit 134d36f

Browse files
committed
feat: add solutions to lc problems: No.1470, 1874
1 parent 90c4571 commit 134d36f

File tree

12 files changed

+266
-36
lines changed

12 files changed

+266
-36
lines changed

solution/1400-1499/1470.Shuffle the Array/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,59 @@
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```python
56-
56+
class Solution:
57+
def shuffle(self, nums: List[int], n: int) -> List[int]:
58+
ans = []
59+
for i in range(n):
60+
ans.append(nums[i])
61+
ans.append(nums[i + n])
62+
return ans
5763
```
5864

5965
### **Java**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```java
70+
class Solution {
71+
public int[] shuffle(int[] nums, int n) {
72+
int[] ans = new int[n << 1];
73+
for (int i = 0, j = 0; i < n; ++i) {
74+
ans[j++] = nums[i];
75+
ans[j++] = nums[i + n];
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
vector<int> shuffle(vector<int>& nums, int n) {
88+
vector<int> ans;
89+
for (int i = 0; i < n; ++i) {
90+
ans.push_back(nums[i]);
91+
ans.push_back(nums[i + n]);
92+
}
93+
return ans;
94+
}
95+
};
96+
```
6497
98+
### **Go**
99+
100+
```go
101+
func shuffle(nums []int, n int) []int {
102+
var ans []int
103+
for i := 0; i < n; i++ {
104+
ans = append(ans, nums[i])
105+
ans = append(ans, nums[i+n])
106+
}
107+
return ans
108+
}
65109
```
66110

67111
### **...**

solution/1400-1499/1470.Shuffle the Array/README_EN.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,57 @@
7777
### **Python3**
7878

7979
```python
80-
80+
class Solution:
81+
def shuffle(self, nums: List[int], n: int) -> List[int]:
82+
ans = []
83+
for i in range(n):
84+
ans.append(nums[i])
85+
ans.append(nums[i + n])
86+
return ans
8187
```
8288

8389
### **Java**
8490

8591
```java
92+
class Solution {
93+
public int[] shuffle(int[] nums, int n) {
94+
int[] ans = new int[n << 1];
95+
for (int i = 0, j = 0; i < n; ++i) {
96+
ans[j++] = nums[i];
97+
ans[j++] = nums[i + n];
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
vector<int> shuffle(vector<int>& nums, int n) {
110+
vector<int> ans;
111+
for (int i = 0; i < n; ++i) {
112+
ans.push_back(nums[i]);
113+
ans.push_back(nums[i + n]);
114+
}
115+
return ans;
116+
}
117+
};
118+
```
86119
120+
### **Go**
121+
122+
```go
123+
func shuffle(nums []int, n int) []int {
124+
var ans []int
125+
for i := 0; i < n; i++ {
126+
ans = append(ans, nums[i])
127+
ans = append(ans, nums[i+n])
128+
}
129+
return ans
130+
}
87131
```
88132

89133
### **...**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> shuffle(vector<int>& nums, int n) {
4+
vector<int> ans;
5+
for (int i = 0; i < n; ++i) {
6+
ans.push_back(nums[i]);
7+
ans.push_back(nums[i + n]);
8+
}
9+
return ans;
10+
}
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func shuffle(nums []int, n int) []int {
2+
var ans []int
3+
for i := 0; i < n; i++ {
4+
ans = append(ans, nums[i])
5+
ans = append(ans, nums[i+n])
6+
}
7+
return ans
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int[] shuffle(int[] nums, int n) {
3+
int[] ans = new int[n << 1];
4+
for (int i = 0, j = 0; i < n; ++i) {
5+
ans[j++] = nums[i];
6+
ans[j++] = nums[i + n];
7+
}
8+
return ans;
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def shuffle(self, nums: List[int], n: int) -> List[int]:
3+
ans = []
4+
for i in range(n):
5+
ans.append(nums[i])
6+
ans.append(nums[i + n])
7+
return ans

solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README.md

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,16 @@
88

99
<p>The <b>product sum </b>of two equal-length arrays <code>a</code> and <code>b</code> is equal to the sum of <code>a[i] * b[i]</code> for all <code>0 &lt;= i &lt; a.length</code> (<strong>0-indexed</strong>).</p>
1010

11-
12-
1311
<ul>
1412
<li>For example, if <code>a = [1,2,3,4]</code> and <code>b = [5,2,3,1]</code>, the <strong>product sum</strong> would be <code>1*5 + 2*2 + 3*3 + 4*1 = 22</code>.</li>
1513
</ul>
1614

17-
18-
1915
<p>Given two arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>, return <em>the <strong>minimum product sum</strong> if you are allowed to <strong>rearrange</strong> the <strong>order</strong> of the elements in </em><code>nums1</code>.&nbsp;</p>
2016

21-
22-
2317
<p>&nbsp;</p>
2418

2519
<p><strong>Example 1:</strong></p>
2620

27-
28-
2921
<pre>
3022

3123
<strong>Input:</strong> nums1 = [5,3,4,2], nums2 = [4,2,2,5]
@@ -36,12 +28,8 @@
3628

3729
</pre>
3830

39-
40-
4131
<p><strong>Example 2:</strong></p>
4232

43-
44-
4533
<pre>
4634

4735
<strong>Input:</strong> nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]
@@ -52,14 +40,10 @@
5240

5341
</pre>
5442

55-
56-
5743
<p>&nbsp;</p>
5844

5945
<p><strong>Constraints:</strong></p>
6046

61-
62-
6347
<ul>
6448
<li><code>n == nums1.length == nums2.length</code></li>
6549
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
@@ -70,22 +54,72 @@
7054

7155
<!-- 这里可写通用的实现逻辑 -->
7256

57+
对两数组排序,然后首尾相乘求和。
58+
7359
<!-- tabs:start -->
7460

7561
### **Python3**
7662

7763
<!-- 这里可写当前语言的特殊实现逻辑 -->
7864

7965
```python
80-
66+
class Solution:
67+
def minProductSum(self, nums1: List[int], nums2: List[int]) -> int:
68+
nums1.sort()
69+
nums2.sort()
70+
n, res = len(nums1), 0
71+
for i in range(n):
72+
res += nums1[i] * nums2[n - i - 1]
73+
return res
8174
```
8275

8376
### **Java**
8477

8578
<!-- 这里可写当前语言的特殊实现逻辑 -->
8679

8780
```java
81+
class Solution {
82+
public int minProductSum(int[] nums1, int[] nums2) {
83+
Arrays.sort(nums1);
84+
Arrays.sort(nums2);
85+
int n = nums1.length, res = 0;
86+
for (int i = 0; i < n; ++i) {
87+
res += nums1[i] * nums2[n - i - 1];
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int minProductSum(vector<int>& nums1, vector<int>& nums2) {
100+
sort(nums1.begin(), nums1.end());
101+
sort(nums2.begin(), nums2.end());
102+
int n = nums1.size(), res = 0;
103+
for (int i = 0; i < n; ++i) {
104+
res += nums1[i] * nums2[n - i - 1];
105+
}
106+
return res;
107+
}
108+
};
109+
```
88110
111+
### **Go**
112+
113+
```go
114+
func minProductSum(nums1 []int, nums2 []int) int {
115+
sort.Ints(nums1)
116+
sort.Ints(nums2)
117+
res, n := 0, len(nums1)
118+
for i, num := range nums1 {
119+
res += num * nums2[n-i-1]
120+
}
121+
return res
122+
}
89123
```
90124

91125
### **...**

0 commit comments

Comments
 (0)