Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aebad32

Browse files
committed
feat: add solutions to lc problem: No.2418
No.2418.Sort the People
1 parent 3d4ff04 commit aebad32

File tree

6 files changed

+207
-60
lines changed

6 files changed

+207
-60
lines changed

‎solution/2400-2499/2418.Sort the People/README.md‎

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545

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

48-
**方法一:直接模拟**
48+
**方法一:排序**
4949

50-
直接按照题意,从高到低遍历身高数组,将对应的名字加入结果数组即可
50+
根据题目描述,我们可以创建一个长度为 $n$ 的下标数组 $idx,ドル其中 $idx[i]=i$。然后我们对 $idx$ 中的每个下标按照 $heights$ 中对应的身高降序排序,最后遍历排序后的 $idx$ 中的每个下标 $i,ドル将 $names[i]$ 加入答案数组即可
5151

52-
时间复杂度 $O(n\log n)$。其中 $n$ 为数组 `heights` 的长度。
52+
我们也可以创建一个长度为 $n$ 的数组 $arr,ドル数组中每个元素是一个二元组 $(heights[i], i),ドル然后我们对 $arr$ 按照身高降序排序。最后遍历排序后的 $arr$ 中的每个元素 $(heights[i], i),ドル将 $names[i]$ 加入答案数组即可。
53+
54+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $names$ 和 $heights$ 的长度。
5355

5456
<!-- tabs:start -->
5557

@@ -65,14 +67,38 @@ class Solution:
6567
return [names[i] for i in idx]
6668
```
6769

70+
```python
71+
class Solution:
72+
def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
73+
return [name for _, name in sorted(zip(heights, names), reverse=True)]
74+
```
75+
6876
### **Java**
6977

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

7280
```java
7381
class Solution {
7482
public String[] sortPeople(String[] names, int[] heights) {
75-
int n = heights.length;
83+
int n = names.length;
84+
Integer[] idx = new Integer[n];
85+
for (int i = 0; i < n; ++i) {
86+
idx[i] = i;
87+
}
88+
Arrays.sort(idx, (i, j) -> heights[j] - heights[i]);
89+
String[] ans = new String[n];
90+
for (int i = 0; i < n; ++i) {
91+
ans[i] = names[idx[i]];
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
```java
99+
class Solution {
100+
public String[] sortPeople(String[] names, int[] heights) {
101+
int n = names.length;
76102
int[][] arr = new int[n][2];
77103
for (int i = 0; i < n; ++i) {
78104
arr[i] = new int[] {heights[i], i};
@@ -93,15 +119,32 @@ class Solution {
93119
class Solution {
94120
public:
95121
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
96-
int n = heights.size();
97-
vector<pair<int, int>> arr(n);
122+
int n = names.size();
123+
vector<int> idx(n);
124+
iota(idx.begin(), idx.end(), 0);
125+
sort(idx.begin(), idx.end(), [&](int i, int j) { return heights[j] < heights[i]; });
126+
vector<string> ans;
127+
for (int i : idx) {
128+
ans.push_back(names[i]);
129+
}
130+
return ans;
131+
}
132+
};
133+
```
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
139+
int n = names.size();
140+
vector<pair<int, int>> arr;
98141
for (int i = 0; i < n; ++i) {
99-
arr[i] = {-heights[i], i};
142+
arr.emplace_back(-heights[i], i);
100143
}
101144
sort(arr.begin(), arr.end());
102-
vector<string> ans(n);
145+
vector<string> ans;
103146
for (int i = 0; i < n; ++i) {
104-
ans[i] = names[arr[i].second];
147+
ans.emplace_back(names[arr[i].second]);
105148
}
106149
return ans;
107150
}
@@ -110,25 +153,55 @@ public:
110153

111154
### **Go**
112155

156+
```go
157+
func sortPeople(names []string, heights []int) (ans []string) {
158+
n := len(names)
159+
idx := make([]int, n)
160+
for i := range idx {
161+
idx[i] = i
162+
}
163+
sort.Slice(idx, func(i, j int) bool { return heights[idx[j]] < heights[idx[i]] })
164+
for _, i := range idx {
165+
ans = append(ans, names[i])
166+
}
167+
return
168+
}
169+
```
170+
113171
```go
114172
func sortPeople(names []string, heights []int) []string {
115-
n := len(heights)
116-
type pair struct{ v, i int }
117-
arr := make([]pair, n)
118-
for i, v := range heights {
119-
arr[i] = pair{v, i}
173+
n := len(names)
174+
arr := make([][2]int, n)
175+
for i, h := range heights {
176+
arr[i] = [2]int{h, i}
120177
}
121-
sort.Slice(arr, func(i, j int) bool { return arr[i].v > arr[j].v })
178+
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
122179
ans := make([]string, n)
123-
for i, v := range arr {
124-
ans[i] = names[v.i]
180+
for i, x := range arr {
181+
ans[i] = names[x[1]]
125182
}
126183
return ans
127184
}
128185
```
129186

130187
### **TypeScript**
131188

189+
```ts
190+
function sortPeople(names: string[], heights: number[]): string[] {
191+
const n = names.length;
192+
const idx = new Array(n);
193+
for (let i = 0; i < n; ++i) {
194+
idx[i] = i;
195+
}
196+
idx.sort((i, j) => heights[j] - heights[i]);
197+
const ans: string[] = [];
198+
for (const i of idx) {
199+
ans.push(names[i]);
200+
}
201+
return ans;
202+
}
203+
```
204+
132205
```ts
133206
function sortPeople(names: string[], heights: number[]): string[] {
134207
return names

‎solution/2400-2499/2418.Sort the People/README_EN.md‎

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,36 @@ class Solution:
5353
return [names[i] for i in idx]
5454
```
5555

56+
```python
57+
class Solution:
58+
def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
59+
return [name for _, name in sorted(zip(heights, names), reverse=True)]
60+
```
61+
5662
### **Java**
5763

5864
```java
5965
class Solution {
6066
public String[] sortPeople(String[] names, int[] heights) {
61-
int n = heights.length;
67+
int n = names.length;
68+
Integer[] idx = new Integer[n];
69+
for (int i = 0; i < n; ++i) {
70+
idx[i] = i;
71+
}
72+
Arrays.sort(idx, (i, j) -> heights[j] - heights[i]);
73+
String[] ans = new String[n];
74+
for (int i = 0; i < n; ++i) {
75+
ans[i] = names[idx[i]];
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
```java
83+
class Solution {
84+
public String[] sortPeople(String[] names, int[] heights) {
85+
int n = names.length;
6286
int[][] arr = new int[n][2];
6387
for (int i = 0; i < n; ++i) {
6488
arr[i] = new int[] {heights[i], i};
@@ -79,15 +103,32 @@ class Solution {
79103
class Solution {
80104
public:
81105
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
82-
int n = heights.size();
83-
vector<pair<int, int>> arr(n);
106+
int n = names.size();
107+
vector<int> idx(n);
108+
iota(idx.begin(), idx.end(), 0);
109+
sort(idx.begin(), idx.end(), [&](int i, int j) { return heights[j] < heights[i]; });
110+
vector<string> ans;
111+
for (int i : idx) {
112+
ans.push_back(names[i]);
113+
}
114+
return ans;
115+
}
116+
};
117+
```
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
123+
int n = names.size();
124+
vector<pair<int, int>> arr;
84125
for (int i = 0; i < n; ++i) {
85-
arr[i] = {-heights[i], i};
126+
arr.emplace_back(-heights[i], i);
86127
}
87128
sort(arr.begin(), arr.end());
88-
vector<string> ans(n);
129+
vector<string> ans;
89130
for (int i = 0; i < n; ++i) {
90-
ans[i] = names[arr[i].second];
131+
ans.emplace_back(names[arr[i].second]);
91132
}
92133
return ans;
93134
}
@@ -96,25 +137,55 @@ public:
96137

97138
### **Go**
98139

140+
```go
141+
func sortPeople(names []string, heights []int) (ans []string) {
142+
n := len(names)
143+
idx := make([]int, n)
144+
for i := range idx {
145+
idx[i] = i
146+
}
147+
sort.Slice(idx, func(i, j int) bool { return heights[idx[j]] < heights[idx[i]] })
148+
for _, i := range idx {
149+
ans = append(ans, names[i])
150+
}
151+
return
152+
}
153+
```
154+
99155
```go
100156
func sortPeople(names []string, heights []int) []string {
101-
n := len(heights)
102-
type pair struct{ v, i int }
103-
arr := make([]pair, n)
104-
for i, v := range heights {
105-
arr[i] = pair{v, i}
157+
n := len(names)
158+
arr := make([][2]int, n)
159+
for i, h := range heights {
160+
arr[i] = [2]int{h, i}
106161
}
107-
sort.Slice(arr, func(i, j int) bool { return arr[i].v > arr[j].v })
162+
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
108163
ans := make([]string, n)
109-
for i, v := range arr {
110-
ans[i] = names[v.i]
164+
for i, x := range arr {
165+
ans[i] = names[x[1]]
111166
}
112167
return ans
113168
}
114169
```
115170

116171
### **TypeScript**
117172

173+
```ts
174+
function sortPeople(names: string[], heights: number[]): string[] {
175+
const n = names.length;
176+
const idx = new Array(n);
177+
for (let i = 0; i < n; ++i) {
178+
idx[i] = i;
179+
}
180+
idx.sort((i, j) => heights[j] - heights[i]);
181+
const ans: string[] = [];
182+
for (const i of idx) {
183+
ans.push(names[i]);
184+
}
185+
return ans;
186+
}
187+
```
188+
118189
```ts
119190
function sortPeople(names: string[], heights: number[]): string[] {
120191
return names

‎solution/2400-2499/2418.Sort the People/Solution.cpp‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public:
33
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
4-
int n = heights.size();
5-
vector<pair<int, int>> arr(n);
6-
for (int i = 0; i < n; ++i) {
7-
arr[i] = {-heights[i], i};
8-
}
9-
sort(arr.begin(), arr.end());
10-
vector<string> ans(n);
11-
for (int i = 0; i < n; ++i) {
12-
ans[i] = names[arr[i].second];
4+
int n = names.size();
5+
vector<int> idx(n);
6+
iota(idx.begin(), idx.end(), 0);
7+
sort(idx.begin(), idx.end(), [&](int i, int j) { return heights[j] < heights[i]; });
8+
vector<string> ans;
9+
for (int i : idx) {
10+
ans.push_back(names[i]);
1311
}
1412
return ans;
1513
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
func sortPeople(names []string, heights []int) []string {
2-
n := len(heights)
3-
type pair struct{ v, i int }
4-
arr := make([]pair, n)
5-
for i, v := range heights {
6-
arr[i] = pair{v, i}
1+
func sortPeople(names []string, heights []int) (ans []string) {
2+
n := len(names)
3+
idx := make([]int, n)
4+
for i := range idx {
5+
idx[i] = i
76
}
8-
sort.Slice(arr, func(i, j int) bool { return arr[i].v > arr[j].v })
9-
ans := make([]string, n)
10-
for i, v := range arr {
11-
ans[i] = names[v.i]
7+
sort.Slice(idx, func(i, j int) bool { return heights[idx[j]] < heights[idx[i]] })
8+
for _, i := range idx {
9+
ans = append(ans, names[i])
1210
}
13-
returnans
11+
return
1412
}

‎solution/2400-2499/2418.Sort the People/Solution.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public String[] sortPeople(String[] names, int[] heights) {
3-
int n = heights.length;
4-
int[][] arr = new int[n][2];
3+
int n = names.length;
4+
Integer[] idx = new Integer[n];
55
for (int i = 0; i < n; ++i) {
6-
arr[i] = newint[] {heights[i], i};
6+
idx[i] = i;
77
}
8-
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
8+
Arrays.sort(idx, (i, j) -> heights[j] - heights[i]);
99
String[] ans = new String[n];
1010
for (int i = 0; i < n; ++i) {
11-
ans[i] = names[arr[i][1]];
11+
ans[i] = names[idx[i]];
1212
}
1313
return ans;
1414
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
function sortPeople(names: string[], heights: number[]): string[] {
2-
return names
3-
.map<[string, number]>((s, i) => [s, heights[i]])
4-
.sort((a, b) => b[1] - a[1])
5-
.map(([v]) => v);
2+
const n = names.length;
3+
const idx = new Array(n);
4+
for (let i = 0; i < n; ++i) {
5+
idx[i] = i;
6+
}
7+
idx.sort((i, j) => heights[j] - heights[i]);
8+
const ans: string[] = [];
9+
for (const i of idx) {
10+
ans.push(names[i]);
11+
}
12+
return ans;
613
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /