Skip to content

Commit c2c2b52

Browse files
committed
feat: add solutions to lc problem: No.1051
No.1051.Height Checker
1 parent 64a143b commit c2c2b52

File tree

6 files changed

+206
-90
lines changed

6 files changed

+206
-90
lines changed

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

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:排序**
63+
64+
将 $heights$ 复制并排序得到 $expected$,然后同时遍历 $heights$, $expected$ ,统计对应位置元素不同的个数。
65+
66+
时间复杂度 $O(nlogn)$,其中 $n$ 表示 $heights$ 的长度。
67+
68+
**方法二:计数排序**
69+
70+
由于题目中学生高度不超过 $100$,因此可以使用计数排序。这里我们用一个长度 $101$ 的数组 $cnt$ 统计每个高度 $h_i$ 出现的次数。
71+
72+
时间复杂度 $(n)$。
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**
@@ -68,24 +80,24 @@
6880
```python
6981
class Solution:
7082
def heightChecker(self, heights: List[int]) -> int:
71-
bucket = [0] * 101
72-
for h in heights:
73-
bucket[h] += 1
74-
res = i = 0
75-
for j in range(1, 101):
76-
while bucket[j] > 0:
77-
bucket[j] -= 1
78-
if heights[i] != j:
79-
res += 1
80-
i += 1
81-
return res
83+
expected = sorted(heights)
84+
return sum(a != b for a, b in zip(heights, expected))
8285
```
8386

8487
```python
8588
class Solution:
8689
def heightChecker(self, heights: List[int]) -> int:
87-
expected = sorted(heights)
88-
return sum(1 for i, h in enumerate(heights) if h != expected[i])
90+
cnt = [0] * 101
91+
for h in heights:
92+
cnt[h] += 1
93+
ans = i = 0
94+
for j in range(1, 101):
95+
while cnt[j]:
96+
cnt[j] -= 1
97+
if heights[i] != j:
98+
ans += 1
99+
i += 1
100+
return ans
89101
```
90102

91103
### **Java**
@@ -95,15 +107,36 @@ class Solution:
95107
```java
96108
class Solution {
97109
public int heightChecker(int[] heights) {
98-
int[] expected = Arrays.copyOf(heights, heights.length);
110+
int[] expected = heights.clone();
99111
Arrays.sort(expected);
100-
int res = 0;
112+
int ans = 0;
101113
for (int i = 0; i < heights.length; ++i) {
102114
if (heights[i] != expected[i]) {
103-
++res;
115+
++ans;
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
```java
124+
class Solution {
125+
public int heightChecker(int[] heights) {
126+
int[] cnt = new int[101];
127+
for (int h : heights) {
128+
++cnt[h];
129+
}
130+
int ans = 0;
131+
for (int i = 0, j = 0; i < 101; ++i) {
132+
while (cnt[i] > 0) {
133+
--cnt[i];
134+
if (heights[j++] != i) {
135+
++ans;
136+
}
104137
}
105138
}
106-
return res;
139+
return ans;
107140
}
108141
}
109142
```
@@ -116,12 +149,29 @@ public:
116149
int heightChecker(vector<int>& heights) {
117150
vector<int> expected = heights;
118151
sort(expected.begin(), expected.end());
119-
int res = 0;
120-
for (int i = 0; i < heights.size(); ++i)
152+
int ans = 0;
153+
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
154+
return ans;
155+
}
156+
};
157+
```
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
int heightChecker(vector<int>& heights) {
163+
vector<int> cnt(101);
164+
for (int& h : heights) ++cnt[h];
165+
int ans = 0;
166+
for (int i = 0, j = 0; i < 101; ++i)
121167
{
122-
if (heights[i] != expected[i]) ++res;
168+
while (cnt[i])
169+
{
170+
--cnt[i];
171+
if (heights[j++] != i) ++ans;
172+
}
123173
}
124-
return res;
174+
return ans;
125175
}
126176
};
127177
```
@@ -130,16 +180,36 @@ public:
130180

131181
```go
132182
func heightChecker(heights []int) int {
133-
expected := make([]int, len(heights))
134-
copy(expected, heights)
135-
sort.Ints(expected)
136-
res := 0
137-
for i, h := range heights {
138-
if h != expected[i] {
139-
res++
140-
}
141-
}
142-
return res
183+
expected := make([]int, len(heights))
184+
copy(expected, heights)
185+
sort.Ints(expected)
186+
ans := 0
187+
for i, v := range heights {
188+
if v != expected[i] {
189+
ans++
190+
}
191+
}
192+
return ans
193+
}
194+
```
195+
196+
```go
197+
func heightChecker(heights []int) int {
198+
cnt := make([]int, 101)
199+
for _, h := range heights {
200+
cnt[h]++
201+
}
202+
ans := 0
203+
for i, j := 0, 0; i < 101; i++ {
204+
for cnt[i] > 0 {
205+
cnt[i]--
206+
if heights[j] != i {
207+
ans++
208+
}
209+
j++
210+
}
211+
}
212+
return ans
143213
}
144214
```
145215

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

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,61 @@ All indices match.
6161
```python
6262
class Solution:
6363
def heightChecker(self, heights: List[int]) -> int:
64-
bucket = [0] * 101
65-
for h in heights:
66-
bucket[h] += 1
67-
res = i = 0
68-
for j in range(1, 101):
69-
while bucket[j] > 0:
70-
bucket[j] -= 1
71-
if heights[i] != j:
72-
res += 1
73-
i += 1
74-
return res
64+
expected = sorted(heights)
65+
return sum(a != b for a, b in zip(heights, expected))
7566
```
7667

7768
```python
7869
class Solution:
7970
def heightChecker(self, heights: List[int]) -> int:
80-
expected = sorted(heights)
81-
return sum(1 for i, h in enumerate(heights) if h != expected[i])
71+
cnt = [0] * 101
72+
for h in heights:
73+
cnt[h] += 1
74+
ans = i = 0
75+
for j in range(1, 101):
76+
while cnt[j]:
77+
cnt[j] -= 1
78+
if heights[i] != j:
79+
ans += 1
80+
i += 1
81+
return ans
8282
```
8383

8484
### **Java**
8585

8686
```java
8787
class Solution {
8888
public int heightChecker(int[] heights) {
89-
int[] expected = Arrays.copyOf(heights, heights.length);
89+
int[] expected = heights.clone();
9090
Arrays.sort(expected);
91-
int res = 0;
91+
int ans = 0;
9292
for (int i = 0; i < heights.length; ++i) {
9393
if (heights[i] != expected[i]) {
94-
++res;
94+
++ans;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
```java
103+
class Solution {
104+
public int heightChecker(int[] heights) {
105+
int[] cnt = new int[101];
106+
for (int h : heights) {
107+
++cnt[h];
108+
}
109+
int ans = 0;
110+
for (int i = 0, j = 0; i < 101; ++i) {
111+
while (cnt[i] > 0) {
112+
--cnt[i];
113+
if (heights[j++] != i) {
114+
++ans;
115+
}
95116
}
96117
}
97-
return res;
118+
return ans;
98119
}
99120
}
100121
```
@@ -107,12 +128,29 @@ public:
107128
int heightChecker(vector<int>& heights) {
108129
vector<int> expected = heights;
109130
sort(expected.begin(), expected.end());
110-
int res = 0;
111-
for (int i = 0; i < heights.size(); ++i)
131+
int ans = 0;
132+
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
133+
return ans;
134+
}
135+
};
136+
```
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int heightChecker(vector<int>& heights) {
142+
vector<int> cnt(101);
143+
for (int& h : heights) ++cnt[h];
144+
int ans = 0;
145+
for (int i = 0, j = 0; i < 101; ++i)
112146
{
113-
if (heights[i] != expected[i]) ++res;
147+
while (cnt[i])
148+
{
149+
--cnt[i];
150+
if (heights[j++] != i) ++ans;
151+
}
114152
}
115-
return res;
153+
return ans;
116154
}
117155
};
118156
```
@@ -121,16 +159,36 @@ public:
121159

122160
```go
123161
func heightChecker(heights []int) int {
124-
expected := make([]int, len(heights))
125-
copy(expected, heights)
126-
sort.Ints(expected)
127-
res := 0
128-
for i, h := range heights {
129-
if h != expected[i] {
130-
res++
131-
}
132-
}
133-
return res
162+
expected := make([]int, len(heights))
163+
copy(expected, heights)
164+
sort.Ints(expected)
165+
ans := 0
166+
for i, v := range heights {
167+
if v != expected[i] {
168+
ans++
169+
}
170+
}
171+
return ans
172+
}
173+
```
174+
175+
```go
176+
func heightChecker(heights []int) int {
177+
cnt := make([]int, 101)
178+
for _, h := range heights {
179+
cnt[h]++
180+
}
181+
ans := 0
182+
for i, j := 0, 0; i < 101; i++ {
183+
for cnt[i] > 0 {
184+
cnt[i]--
185+
if heights[j] != i {
186+
ans++
187+
}
188+
j++
189+
}
190+
}
191+
return ans
134192
}
135193
```
136194

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution {
22
public:
33
int heightChecker(vector<int>& heights) {
4-
vector<int> copy = heights;
5-
sort(copy.begin(), copy.end());
6-
int res = 0;
7-
for (int i = 0; i < heights.size(); ++i)
8-
{
9-
if (heights[i] != copy[i]) ++res;
10-
}
11-
return res;
4+
vector<int> expected = heights;
5+
sort(expected.begin(), expected.end());
6+
int ans = 0;
7+
for (int i = 0; i < heights.size(); ++i) ans += heights[i] != expected[i];
8+
return ans;
129
}
1310
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ func heightChecker(heights []int) int {
22
expected := make([]int, len(heights))
33
copy(expected, heights)
44
sort.Ints(expected)
5-
res := 0
6-
for i, h := range heights {
7-
if h != expected[i] {
8-
res++
5+
ans := 0
6+
for i, v := range heights {
7+
if v != expected[i] {
8+
ans++
99
}
1010
}
11-
return res
11+
return ans
1212
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int heightChecker(int[] heights) {
3-
int[] expected = Arrays.copyOf(heights, heights.length);
3+
int[] expected = heights.clone();
44
Arrays.sort(expected);
5-
int res = 0;
5+
int ans = 0;
66
for (int i = 0; i < heights.length; ++i) {
77
if (heights[i] != expected[i]) {
8-
++res;
8+
++ans;
99
}
1010
}
11-
return res;
11+
return ans;
1212
}
1313
}

0 commit comments

Comments
 (0)