Skip to content

feat: add ts solution to lc problem: No.1122 #3091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 11, 2024
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
152 changes: 152 additions & 0 deletions solution/1100-1199/1122.Relative Sort Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,156 @@ function relativeSortArray(arr1: number[], arr2: number[]): number[] {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:计数排序

我们可以使用计数排序的思想,首先统计数组 $arr1$ 中每个元素的出现次数,然后按照数组 $arr2$ 中的顺序,将 $arr1$ 中的元素按照出现次数放入答案数组 $ans$ 中。最后,我们遍历 $arr1$ 中的所有元素,将未在 $arr2$ 中出现的元素按照升序放入答案数组 $ans$ 的末尾。

时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $arr1$ 和 $arr2$ 的长度。

<!-- solution:start -->

#### Python3

```python
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
cnt = Counter(arr1)
ans = []
for x in arr2:
ans.extend([x] * cnt[x])
cnt.pop(x)
mi, mx = min(arr1), max(arr1)
for x in range(mi, mx + 1):
ans.extend([x] * cnt[x])
return ans
```

#### Java

```java
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] cnt = new int[1001];
int mi = 1001, mx = 0;
for (int x : arr1) {
++cnt[x];
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
int m = arr1.length;
int[] ans = new int[m];
int i = 0;
for (int x : arr2) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
for (int x = mi; x <= mx; ++x) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> cnt(1001);
for (int x : arr1) {
++cnt[x];
}
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
vector<int> ans;
for (int x : arr2) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
for (int x = *mi; x <= *mx; ++x) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
return ans;
}
};
```

#### Go

```go
func relativeSortArray(arr1 []int, arr2 []int) []int {
cnt := make([]int, 1001)
mi, mx := 1001, 0
for _, x := range arr1 {
cnt[x]++
mi = min(mi, x)
mx = max(mx, x)
}
ans := make([]int, 0, len(arr1))
for _, x := range arr2 {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
for x := mi; x <= mx; x++ {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
return ans
}
```

#### TypeScript

```ts
function relativeSortArray(arr1: number[], arr2: number[]): number[] {
const cnt = Array(1001).fill(0);
let mi = Number.POSITIVE_INFINITY;
let mx = Number.NEGATIVE_INFINITY;

for (const x of arr1) {
cnt[x]++;
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}

const ans: number[] = [];
for (const x of arr2) {
while (cnt[x]) {
cnt[x]--;
ans.push(x);
}
}

for (let i = mi; i <= mx; i++) {
while (cnt[i]) {
cnt[i]--;
ans.push(i);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
152 changes: 152 additions & 0 deletions solution/1100-1199/1122.Relative Sort Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,156 @@ function relativeSortArray(arr1: number[], arr2: number[]): number[] {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Counting Sort

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

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.

<!-- solution:start -->

#### Python3

```python
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
cnt = Counter(arr1)
ans = []
for x in arr2:
ans.extend([x] * cnt[x])
cnt.pop(x)
mi, mx = min(arr1), max(arr1)
for x in range(mi, mx + 1):
ans.extend([x] * cnt[x])
return ans
```

#### Java

```java
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] cnt = new int[1001];
int mi = 1001, mx = 0;
for (int x : arr1) {
++cnt[x];
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
int m = arr1.length;
int[] ans = new int[m];
int i = 0;
for (int x : arr2) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
for (int x = mi; x <= mx; ++x) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> cnt(1001);
for (int x : arr1) {
++cnt[x];
}
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
vector<int> ans;
for (int x : arr2) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
for (int x = *mi; x <= *mx; ++x) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
return ans;
}
};
```

#### Go

```go
func relativeSortArray(arr1 []int, arr2 []int) []int {
cnt := make([]int, 1001)
mi, mx := 1001, 0
for _, x := range arr1 {
cnt[x]++
mi = min(mi, x)
mx = max(mx, x)
}
ans := make([]int, 0, len(arr1))
for _, x := range arr2 {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
for x := mi; x <= mx; x++ {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
return ans
}
```

#### TypeScript

```ts
function relativeSortArray(arr1: number[], arr2: number[]): number[] {
const cnt = Array(1001).fill(0);
let mi = Number.POSITIVE_INFINITY;
let mx = Number.NEGATIVE_INFINITY;

for (const x of arr1) {
cnt[x]++;
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}

const ans: number[] = [];
for (const x of arr2) {
while (cnt[x]) {
cnt[x]--;
ans.push(x);
}
}

for (let i = mi; i <= mx; i++) {
while (cnt[i]) {
cnt[i]--;
ans.push(i);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
24 changes: 24 additions & 0 deletions solution/1100-1199/1122.Relative Sort Array/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> cnt(1001);
for (int x : arr1) {
++cnt[x];
}
auto [mi, mx] = minmax_element(arr1.begin(), arr1.end());
vector<int> ans;
for (int x : arr2) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
for (int x = *mi; x <= *mx; ++x) {
while (cnt[x]) {
ans.push_back(x);
--cnt[x];
}
}
return ans;
}
};
23 changes: 23 additions & 0 deletions solution/1100-1199/1122.Relative Sort Array/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func relativeSortArray(arr1 []int, arr2 []int) []int {
cnt := make([]int, 1001)
mi, mx := 1001, 0
for _, x := range arr1 {
cnt[x]++
mi = min(mi, x)
mx = max(mx, x)
}
ans := make([]int, 0, len(arr1))
for _, x := range arr2 {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
for x := mi; x <= mx; x++ {
for cnt[x] > 0 {
ans = append(ans, x)
cnt[x]--
}
}
return ans
}
27 changes: 27 additions & 0 deletions solution/1100-1199/1122.Relative Sort Array/Solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] cnt = new int[1001];
int mi = 1001, mx = 0;
for (int x : arr1) {
++cnt[x];
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
int m = arr1.length;
int[] ans = new int[m];
int i = 0;
for (int x : arr2) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
for (int x = mi; x <= mx; ++x) {
while (cnt[x] > 0) {
--cnt[x];
ans[i++] = x;
}
}
return ans;
}
}
Loading