Skip to content

Commit ee9a1b2

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.1122 (#3091)
* feat: add ts solution to lc problem: No.1122 * Update README.md * Update README_EN.md * Update Solution2.ts * Create Solution2.py * Create Solution2.java * Create Solution2.cpp * Create Solution2.go * style: format code and docs with prettier --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 329f894 commit ee9a1b2

File tree

7 files changed

+417
-0
lines changed

7 files changed

+417
-0
lines changed

solution/1100-1199/1122.Relative Sort Array/README.md

+152
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,156 @@ function relativeSortArray(arr1: number[], arr2: number[]): number[] {
169169

170170
<!-- solution:end -->
171171

172+
<!-- solution:start -->
173+
174+
### 方法二:计数排序
175+
176+
我们可以使用计数排序的思想,首先统计数组 $arr1$ 中每个元素的出现次数,然后按照数组 $arr2$ 中的顺序,将 $arr1$ 中的元素按照出现次数放入答案数组 $ans$ 中。最后,我们遍历 $arr1$ 中的所有元素,将未在 $arr2$ 中出现的元素按照升序放入答案数组 $ans$ 的末尾。
177+
178+
时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $arr1$ 和 $arr2$ 的长度。
179+
180+
<!-- solution:start -->
181+
182+
#### Python3
183+
184+
```python
185+
class Solution:
186+
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
187+
cnt = Counter(arr1)
188+
ans = []
189+
for x in arr2:
190+
ans.extend([x] * cnt[x])
191+
cnt.pop(x)
192+
mi, mx = min(arr1), max(arr1)
193+
for x in range(mi, mx + 1):
194+
ans.extend([x] * cnt[x])
195+
return ans
196+
```
197+
198+
#### Java
199+
200+
```java
201+
class Solution {
202+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
203+
int[] cnt = new int[1001];
204+
int mi = 1001, mx = 0;
205+
for (int x : arr1) {
206+
++cnt[x];
207+
mi = Math.min(mi, x);
208+
mx = Math.max(mx, x);
209+
}
210+
int m = arr1.length;
211+
int[] ans = new int[m];
212+
int i = 0;
213+
for (int x : arr2) {
214+
while (cnt[x] > 0) {
215+
--cnt[x];
216+
ans[i++] = x;
217+
}
218+
}
219+
for (int x = mi; x <= mx; ++x) {
220+
while (cnt[x] > 0) {
221+
--cnt[x];
222+
ans[i++] = x;
223+
}
224+
}
225+
return ans;
226+
}
227+
}
228+
```
229+
230+
#### C++
231+
232+
```cpp
233+
class Solution {
234+
public:
235+
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
236+
vector<int> cnt(1001);
237+
for (int x : arr1) {
238+
++cnt[x];
239+
}
240+
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
241+
vector<int> ans;
242+
for (int x : arr2) {
243+
while (cnt[x]) {
244+
ans.push_back(x);
245+
--cnt[x];
246+
}
247+
}
248+
for (int x = *mi; x <= *mx; ++x) {
249+
while (cnt[x]) {
250+
ans.push_back(x);
251+
--cnt[x];
252+
}
253+
}
254+
return ans;
255+
}
256+
};
257+
```
258+
259+
#### Go
260+
261+
```go
262+
func relativeSortArray(arr1 []int, arr2 []int) []int {
263+
cnt := make([]int, 1001)
264+
mi, mx := 1001, 0
265+
for _, x := range arr1 {
266+
cnt[x]++
267+
mi = min(mi, x)
268+
mx = max(mx, x)
269+
}
270+
ans := make([]int, 0, len(arr1))
271+
for _, x := range arr2 {
272+
for cnt[x] > 0 {
273+
ans = append(ans, x)
274+
cnt[x]--
275+
}
276+
}
277+
for x := mi; x <= mx; x++ {
278+
for cnt[x] > 0 {
279+
ans = append(ans, x)
280+
cnt[x]--
281+
}
282+
}
283+
return ans
284+
}
285+
```
286+
287+
#### TypeScript
288+
289+
```ts
290+
function relativeSortArray(arr1: number[], arr2: number[]): number[] {
291+
const cnt = Array(1001).fill(0);
292+
let mi = Number.POSITIVE_INFINITY;
293+
let mx = Number.NEGATIVE_INFINITY;
294+
295+
for (const x of arr1) {
296+
cnt[x]++;
297+
mi = Math.min(mi, x);
298+
mx = Math.max(mx, x);
299+
}
300+
301+
const ans: number[] = [];
302+
for (const x of arr2) {
303+
while (cnt[x]) {
304+
cnt[x]--;
305+
ans.push(x);
306+
}
307+
}
308+
309+
for (let i = mi; i <= mx; i++) {
310+
while (cnt[i]) {
311+
cnt[i]--;
312+
ans.push(i);
313+
}
314+
}
315+
316+
return ans;
317+
}
318+
```
319+
320+
<!-- tabs:end -->
321+
322+
<!-- solution:end -->
323+
172324
<!-- problem:end -->

solution/1100-1199/1122.Relative Sort Array/README_EN.md

+152
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,156 @@ function relativeSortArray(arr1: number[], arr2: number[]): number[] {
167167

168168
<!-- solution:end -->
169169

170+
<!-- solution:start -->
171+
172+
### Solution 2: Counting Sort
173+
174+
We can use the idea of counting sort. First, count the occurrence of each element in array $arr1$. Then, according to the order in array $arr2$, put the elements in $arr1$ into the answer array $ans$ according to their occurrence. Finally, we traverse all elements in $arr1$ and put the elements that do not appear in $arr2$ in ascending order at the end of the answer array $ans$.
175+
176+
The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of arrays $arr1$ and $arr2$ respectively.
177+
178+
<!-- solution:start -->
179+
180+
#### Python3
181+
182+
```python
183+
class Solution:
184+
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
185+
cnt = Counter(arr1)
186+
ans = []
187+
for x in arr2:
188+
ans.extend([x] * cnt[x])
189+
cnt.pop(x)
190+
mi, mx = min(arr1), max(arr1)
191+
for x in range(mi, mx + 1):
192+
ans.extend([x] * cnt[x])
193+
return ans
194+
```
195+
196+
#### Java
197+
198+
```java
199+
class Solution {
200+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
201+
int[] cnt = new int[1001];
202+
int mi = 1001, mx = 0;
203+
for (int x : arr1) {
204+
++cnt[x];
205+
mi = Math.min(mi, x);
206+
mx = Math.max(mx, x);
207+
}
208+
int m = arr1.length;
209+
int[] ans = new int[m];
210+
int i = 0;
211+
for (int x : arr2) {
212+
while (cnt[x] > 0) {
213+
--cnt[x];
214+
ans[i++] = x;
215+
}
216+
}
217+
for (int x = mi; x <= mx; ++x) {
218+
while (cnt[x] > 0) {
219+
--cnt[x];
220+
ans[i++] = x;
221+
}
222+
}
223+
return ans;
224+
}
225+
}
226+
```
227+
228+
#### C++
229+
230+
```cpp
231+
class Solution {
232+
public:
233+
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
234+
vector<int> cnt(1001);
235+
for (int x : arr1) {
236+
++cnt[x];
237+
}
238+
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
239+
vector<int> ans;
240+
for (int x : arr2) {
241+
while (cnt[x]) {
242+
ans.push_back(x);
243+
--cnt[x];
244+
}
245+
}
246+
for (int x = *mi; x <= *mx; ++x) {
247+
while (cnt[x]) {
248+
ans.push_back(x);
249+
--cnt[x];
250+
}
251+
}
252+
return ans;
253+
}
254+
};
255+
```
256+
257+
#### Go
258+
259+
```go
260+
func relativeSortArray(arr1 []int, arr2 []int) []int {
261+
cnt := make([]int, 1001)
262+
mi, mx := 1001, 0
263+
for _, x := range arr1 {
264+
cnt[x]++
265+
mi = min(mi, x)
266+
mx = max(mx, x)
267+
}
268+
ans := make([]int, 0, len(arr1))
269+
for _, x := range arr2 {
270+
for cnt[x] > 0 {
271+
ans = append(ans, x)
272+
cnt[x]--
273+
}
274+
}
275+
for x := mi; x <= mx; x++ {
276+
for cnt[x] > 0 {
277+
ans = append(ans, x)
278+
cnt[x]--
279+
}
280+
}
281+
return ans
282+
}
283+
```
284+
285+
#### TypeScript
286+
287+
```ts
288+
function relativeSortArray(arr1: number[], arr2: number[]): number[] {
289+
const cnt = Array(1001).fill(0);
290+
let mi = Number.POSITIVE_INFINITY;
291+
let mx = Number.NEGATIVE_INFINITY;
292+
293+
for (const x of arr1) {
294+
cnt[x]++;
295+
mi = Math.min(mi, x);
296+
mx = Math.max(mx, x);
297+
}
298+
299+
const ans: number[] = [];
300+
for (const x of arr2) {
301+
while (cnt[x]) {
302+
cnt[x]--;
303+
ans.push(x);
304+
}
305+
}
306+
307+
for (let i = mi; i <= mx; i++) {
308+
while (cnt[i]) {
309+
cnt[i]--;
310+
ans.push(i);
311+
}
312+
}
313+
314+
return ans;
315+
}
316+
```
317+
318+
<!-- tabs:end -->
319+
320+
<!-- solution:end -->
321+
170322
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
4+
vector<int> cnt(1001);
5+
for (int x : arr1) {
6+
++cnt[x];
7+
}
8+
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
9+
vector<int> ans;
10+
for (int x : arr2) {
11+
while (cnt[x]) {
12+
ans.push_back(x);
13+
--cnt[x];
14+
}
15+
}
16+
for (int x = *mi; x <= *mx; ++x) {
17+
while (cnt[x]) {
18+
ans.push_back(x);
19+
--cnt[x];
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func relativeSortArray(arr1 []int, arr2 []int) []int {
2+
cnt := make([]int, 1001)
3+
mi, mx := 1001, 0
4+
for _, x := range arr1 {
5+
cnt[x]++
6+
mi = min(mi, x)
7+
mx = max(mx, x)
8+
}
9+
ans := make([]int, 0, len(arr1))
10+
for _, x := range arr2 {
11+
for cnt[x] > 0 {
12+
ans = append(ans, x)
13+
cnt[x]--
14+
}
15+
}
16+
for x := mi; x <= mx; x++ {
17+
for cnt[x] > 0 {
18+
ans = append(ans, x)
19+
cnt[x]--
20+
}
21+
}
22+
return ans
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
3+
int[] cnt = new int[1001];
4+
int mi = 1001, mx = 0;
5+
for (int x : arr1) {
6+
++cnt[x];
7+
mi = Math.min(mi, x);
8+
mx = Math.max(mx, x);
9+
}
10+
int m = arr1.length;
11+
int[] ans = new int[m];
12+
int i = 0;
13+
for (int x : arr2) {
14+
while (cnt[x] > 0) {
15+
--cnt[x];
16+
ans[i++] = x;
17+
}
18+
}
19+
for (int x = mi; x <= mx; ++x) {
20+
while (cnt[x] > 0) {
21+
--cnt[x];
22+
ans[i++] = x;
23+
}
24+
}
25+
return ans;
26+
}
27+
}

0 commit comments

Comments
 (0)