Skip to content

Commit 329f894

Browse files
authored
feat: update solutions to lc problem: No.1051 (doocs#3090)
No.1051.Height Checker
1 parent c2a4479 commit 329f894

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

solution/1000-1099/1051.Height Checker/README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ tags:
7777

7878
### 方法一:排序
7979

80-
将 $heights$ 复制并排序得到 $expected$,然后同时遍历 $heights$, $expected$ ,统计对应位置元素不同的个数
80+
我们可以先对学生的高度进行排序,然后比较排序后的高度和原始高度,统计不同的位置即可
8181

82-
时间复杂度 $O(nlogn)$,其中 $n$ 表示 $heights$ 的长度
82+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是学生的数量
8383

8484
<!-- tabs:start -->
8585

@@ -119,7 +119,9 @@ public:
119119
vector<int> expected = heights;
120120
sort(expected.begin(), expected.end());
121121
int ans = 0;
122-
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
122+
for (int i = 0; i < heights.size(); ++i) {
123+
ans += heights[i] != expected[i];
124+
}
123125
return ans;
124126
}
125127
};
@@ -128,17 +130,15 @@ public:
128130
#### Go
129131
130132
```go
131-
func heightChecker(heights []int) int {
132-
expected := make([]int, len(heights))
133-
copy(expected, heights)
133+
func heightChecker(heights []int) (ans int) {
134+
expected := slices.Clone(heights)
134135
sort.Ints(expected)
135-
ans := 0
136136
for i, v := range heights {
137137
if v != expected[i] {
138138
ans++
139139
}
140140
}
141-
return ans
141+
return
142142
}
143143
```
144144

@@ -161,7 +161,7 @@ function heightChecker(heights: number[]): number {
161161

162162
由于题目中学生高度不超过 $100$,因此可以使用计数排序。这里我们用一个长度 $101$ 的数组 $cnt$ 统计每个高度 $h_i$ 出现的次数。
163163

164-
时间复杂度 $(n)$。
164+
时间复杂度 $O(n + M)$,空间复杂度 $O(M)$。其中 $n$ 是学生的数量,而 $M$ 是学生的最大高度,本题中 $M = 101$。
165165

166166
<!-- tabs:start -->
167167

@@ -253,16 +253,17 @@ func heightChecker(heights []int) int {
253253
```ts
254254
function heightChecker(heights: number[]): number {
255255
const cnt = Array(101).fill(0);
256-
for (const i of heights) cnt[i]++;
257-
256+
for (const i of heights) {
257+
cnt[i]++;
258+
}
258259
let ans = 0;
259260
for (let j = 1, i = 0; j < 101; j++) {
260-
while (cnt[j]--)
261+
while (cnt[j]--) {
261262
if (heights[i++] !== j) {
262263
ans++;
263264
}
265+
}
264266
}
265-
266267
return ans;
267268
}
268269
```

solution/1000-1099/1051.Height Checker/README_EN.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ All indices match.
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Sorting
78+
79+
We can first sort the heights of the students, then compare the sorted heights with the original heights, and count the positions that are different.
80+
81+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of students.
7882

7983
<!-- tabs:start -->
8084

@@ -114,7 +118,9 @@ public:
114118
vector<int> expected = heights;
115119
sort(expected.begin(), expected.end());
116120
int ans = 0;
117-
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
121+
for (int i = 0; i < heights.size(); ++i) {
122+
ans += heights[i] != expected[i];
123+
}
118124
return ans;
119125
}
120126
};
@@ -123,17 +129,15 @@ public:
123129
#### Go
124130
125131
```go
126-
func heightChecker(heights []int) int {
127-
expected := make([]int, len(heights))
128-
copy(expected, heights)
132+
func heightChecker(heights []int) (ans int) {
133+
expected := slices.Clone(heights)
129134
sort.Ints(expected)
130-
ans := 0
131135
for i, v := range heights {
132136
if v != expected[i] {
133137
ans++
134138
}
135139
}
136-
return ans
140+
return
137141
}
138142
```
139143

@@ -152,7 +156,11 @@ function heightChecker(heights: number[]): number {
152156

153157
<!-- solution:start -->
154158

155-
### Solution 2
159+
### Solution 2: Counting Sort
160+
161+
Since the height of the students in the problem does not exceed $100$, we can use counting sort. Here we use an array $cnt$ of length $101$ to count the number of times each height $h_i$ appears.
162+
163+
The time complexity is $O(n + M)$, and the space complexity is $O(M)$. Where $n$ is the number of students, and $M$ is the maximum height of the students. In this problem, $M = 101$.
156164

157165
<!-- tabs:start -->
158166

@@ -244,16 +252,17 @@ func heightChecker(heights []int) int {
244252
```ts
245253
function heightChecker(heights: number[]): number {
246254
const cnt = Array(101).fill(0);
247-
for (const i of heights) cnt[i]++;
248-
255+
for (const i of heights) {
256+
cnt[i]++;
257+
}
249258
let ans = 0;
250259
for (let j = 1, i = 0; j < 101; j++) {
251-
while (cnt[j]--)
260+
while (cnt[j]--) {
252261
if (heights[i++] !== j) {
253262
ans++;
254263
}
264+
}
255265
}
256-
257266
return ans;
258267
}
259268
```

solution/1000-1099/1051.Height Checker/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class Solution {
44
vector<int> expected = heights;
55
sort(expected.begin(), expected.end());
66
int ans = 0;
7-
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
7+
for (int i = 0; i < heights.size(); ++i) {
8+
ans += heights[i] != expected[i];
9+
}
810
return ans;
911
}
1012
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
func heightChecker(heights []int) int {
2-
expected := make([]int, len(heights))
3-
copy(expected, heights)
1+
func heightChecker(heights []int) (ans int) {
2+
expected := slices.Clone(heights)
43
sort.Ints(expected)
5-
ans := 0
64
for i, v := range heights {
75
if v != expected[i] {
86
ans++
97
}
108
}
11-
return ans
9+
return
1210
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
function heightChecker(heights: number[]): number {
22
const cnt = Array(101).fill(0);
3-
for (const i of heights) cnt[i]++;
4-
3+
for (const i of heights) {
4+
cnt[i]++;
5+
}
56
let ans = 0;
67
for (let j = 1, i = 0; j < 101; j++) {
7-
while (cnt[j]--)
8+
while (cnt[j]--) {
89
if (heights[i++] !== j) {
910
ans++;
1011
}
12+
}
1113
}
12-
1314
return ans;
1415
}

0 commit comments

Comments
 (0)