Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.1470, 1874
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jul 22, 2021
1 parent 90c4571 commit 134d36f
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 36 deletions.
46 changes: 45 additions & 1 deletion solution/1400-1499/1470.Shuffle the Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,59 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
ans = []
for i in range(n):
ans.append(nums[i])
ans.append(nums[i + n])
return ans
```

### **Java**

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

```java
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[n << 1];
for (int i = 0, j = 0; i < n; ++i) {
ans[j++] = nums[i];
ans[j++] = nums[i + n];
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> ans;
for (int i = 0; i < n; ++i) {
ans.push_back(nums[i]);
ans.push_back(nums[i + n]);
}
return ans;
}
};
```
### **Go**
```go
func shuffle(nums []int, n int) []int {
var ans []int
for i := 0; i < n; i++ {
ans = append(ans, nums[i])
ans = append(ans, nums[i+n])
}
return ans
}
```

### **...**
Expand Down
46 changes: 45 additions & 1 deletion solution/1400-1499/1470.Shuffle the Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,57 @@
### **Python3**

```python

class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
ans = []
for i in range(n):
ans.append(nums[i])
ans.append(nums[i + n])
return ans
```

### **Java**

```java
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[n << 1];
for (int i = 0, j = 0; i < n; ++i) {
ans[j++] = nums[i];
ans[j++] = nums[i + n];
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> ans;
for (int i = 0; i < n; ++i) {
ans.push_back(nums[i]);
ans.push_back(nums[i + n]);
}
return ans;
}
};
```
### **Go**
```go
func shuffle(nums []int, n int) []int {
var ans []int
for i := 0; i < n; i++ {
ans = append(ans, nums[i])
ans = append(ans, nums[i+n])
}
return ans
}
```

### **...**
Expand Down
11 changes: 11 additions & 0 deletions solution/1400-1499/1470.Shuffle the Array/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> ans;
for (int i = 0; i < n; ++i) {
ans.push_back(nums[i]);
ans.push_back(nums[i + n]);
}
return ans;
}
};
8 changes: 8 additions & 0 deletions solution/1400-1499/1470.Shuffle the Array/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func shuffle(nums []int, n int) []int {
var ans []int
for i := 0; i < n; i++ {
ans = append(ans, nums[i])
ans = append(ans, nums[i+n])
}
return ans
}
10 changes: 10 additions & 0 deletions solution/1400-1499/1470.Shuffle the Array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[n << 1];
for (int i = 0, j = 0; i < n; ++i) {
ans[j++] = nums[i];
ans[j++] = nums[i + n];
}
return ans;
}
}
7 changes: 7 additions & 0 deletions solution/1400-1499/1470.Shuffle the Array/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
ans = []
for i in range(n):
ans.append(nums[i])
ans.append(nums[i + n])
return ans
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@

<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>



<ul>
<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>
</ul>



<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>



<p>&nbsp;</p>

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



<pre>

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

</pre>



<p><strong>Example 2:</strong></p>



<pre>

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

</pre>



<p>&nbsp;</p>

<p><strong>Constraints:</strong></p>



<ul>
<li><code>n == nums1.length == nums2.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
Expand All @@ -70,22 +54,72 @@

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

对两数组排序,然后首尾相乘求和。

<!-- tabs:start -->

### **Python3**

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

```python

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
```

### **Java**

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

```java
class Solution {
public int minProductSum(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int n = nums1.length, res = 0;
for (int i = 0; i < n; ++i) {
res += nums1[i] * nums2[n - i - 1];
}
return res;
}
}
```

### **C++**

```cpp
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;
for (int i = 0; i < n; ++i) {
res += nums1[i] * nums2[n - i - 1];
}
return res;
}
};
```
### **Go**
```go
func minProductSum(nums1 []int, nums2 []int) int {
sort.Ints(nums1)
sort.Ints(nums2)
res, n := 0, len(nums1)
for i, num := range nums1 {
res += num * nums2[n-i-1]
}
return res
}
```

### **...**
Expand Down
Loading

0 comments on commit 134d36f

Please sign in to comment.