Skip to content

Commit 3b6cd5a

Browse files
committed
feat: add solutions to lc problem: NO.0406
No.0406.Queue Reconstruction by Height
1 parent 7cc55e6 commit 3b6cd5a

File tree

6 files changed

+138
-9
lines changed

6 files changed

+138
-9
lines changed

solution/0400-0499/0406.Queue Reconstruction by Height/README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,77 @@
4848
<li>题目数据确保队列可以被重建</li>
4949
</ul>
5050

51-
5251
## 解法
5352

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

55+
对 people 按照身高降序排列,若身高相同,则按照人数 k 升序排列。然后按照索引位置依次将 people 插入到结果列表中即可。
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

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

6263
```python
63-
64+
class Solution:
65+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
66+
people.sort(key=lambda x: (-x[0], x[1]))
67+
ans = []
68+
for p in people:
69+
ans.insert(p[1], p)
70+
return ans
6471
```
6572

6673
### **Java**
6774

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

7077
```java
78+
class Solution {
79+
public int[][] reconstructQueue(int[][] people) {
80+
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
81+
List<int[]> ans = new ArrayList<>(people.length);
82+
for (int[] p : people) {
83+
ans.add(p[1], p);
84+
}
85+
return ans.toArray(new int[ans.size()][]);
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
96+
sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {
97+
return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);
98+
});
99+
vector<vector<int>> ans;
100+
for (const vector<int>& p: people)
101+
ans.insert(ans.begin() + p[1], p);
102+
return ans;
103+
}
104+
};
105+
```
71106
107+
### **Go**
108+
109+
```go
110+
func reconstructQueue(people [][]int) [][]int {
111+
sort.Slice(people, func(i, j int) bool {
112+
a, b := people[i], people[j]
113+
return a[0] > b[0] || a[0] == b[0] && a[1] < b[1]
114+
})
115+
var ans [][]int
116+
for _, p := range people {
117+
i := p[1]
118+
ans = append(ans[:i], append([][]int{p}, ans[i:]...)...)
119+
}
120+
return ans
121+
}
72122
```
73123

74124
### **...**

solution/0400-0499/0406.Queue Reconstruction by Height/README_EN.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,69 @@ Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
4141
<li>It is guaranteed that the queue can be reconstructed.</li>
4242
</ul>
4343

44-
4544
## Solutions
4645

4746
<!-- tabs:start -->
4847

4948
### **Python3**
5049

5150
```python
52-
51+
class Solution:
52+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
53+
people.sort(key=lambda x: (-x[0], x[1]))
54+
ans = []
55+
for p in people:
56+
ans.insert(p[1], p)
57+
return ans
5358
```
5459

5560
### **Java**
5661

5762
```java
63+
class Solution {
64+
public int[][] reconstructQueue(int[][] people) {
65+
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
66+
List<int[]> ans = new ArrayList<>(people.length);
67+
for (int[] p : people) {
68+
ans.add(p[1], p);
69+
}
70+
return ans.toArray(new int[ans.size()][]);
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
81+
sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {
82+
return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);
83+
});
84+
vector<vector<int>> ans;
85+
for (const vector<int>& p: people)
86+
ans.insert(ans.begin() + p[1], p);
87+
return ans;
88+
}
89+
};
90+
```
5891
92+
### **Go**
93+
94+
```go
95+
func reconstructQueue(people [][]int) [][]int {
96+
sort.Slice(people, func(i, j int) bool {
97+
a, b := people[i], people[j]
98+
return a[0] > b[0] || a[0] == b[0] && a[1] < b[1]
99+
})
100+
var ans [][]int
101+
for _, p := range people {
102+
i := p[1]
103+
ans = append(ans[:i], append([][]int{p}, ans[i:]...)...)
104+
}
105+
return ans
106+
}
59107
```
60108

61109
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
4+
sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b) {
5+
return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);
6+
});
7+
vector<vector<int>> ans;
8+
for (const vector<int>& p: people)
9+
ans.insert(ans.begin() + p[1], p);
10+
return ans;
11+
}
12+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func reconstructQueue(people [][]int) [][]int {
2+
sort.Slice(people, func(i, j int) bool {
3+
a, b := people[i], people[j]
4+
return a[0] > b[0] || a[0] == b[0] && a[1] < b[1]
5+
})
6+
var ans [][]int
7+
for _, p := range people {
8+
i := p[1]
9+
ans = append(ans[:i], append([][]int{p}, ans[i:]...)...)
10+
}
11+
return ans
12+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public int[][] reconstructQueue(int[][] people) {
3-
Arrays.sort(people, (o1, o2) -> o1[0] != o2[0] ? Integer.compare(o2[0], o1[0]) : Integer.compare(o1[1], o2[1]));
4-
List<int[]> res = new ArrayList<>(people.length);
3+
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
4+
List<int[]> ans = new ArrayList<>(people.length);
55
for (int[] p : people) {
6-
res.add(p[1], p);
6+
ans.add(p[1], p);
77
}
8-
return res.toArray(new int[res.size()][]);
8+
return ans.toArray(new int[ans.size()][]);
99
}
10-
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
3+
people.sort(key=lambda x: (-x[0], x[1]))
4+
ans = []
5+
for p in people:
6+
ans.insert(p[1], p)
7+
return ans

0 commit comments

Comments
 (0)