Skip to content

Commit b680b61

Browse files
committed
feat: add solutions to lc problem: No.1051.Height Checker
1 parent f4ccd19 commit b680b61

File tree

8 files changed

+176
-8
lines changed

8 files changed

+176
-8
lines changed

lcof2/剑指 Offer II 118. 多余的边/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ d[find(a)] = distance
122122
```python
123123
class Solution:
124124
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
125-
p = [i for i in range(1010)]
125+
p = list(range(1010))
126126

127127
def find(x):
128128
if p[x] != x:

lcof2/剑指 Offer II 118. 多余的边/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
3-
p = [i for i in range(1010)]
3+
p = list(range(1010))
44

55
def find(x):
66
if p[x] != x:

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
<li><code>1 <= heights[i] <= 100</code></li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
@@ -61,15 +60,81 @@
6160
<!-- 这里可写当前语言的特殊实现逻辑 -->
6261

6362
```python
63+
class Solution:
64+
def heightChecker(self, heights: List[int]) -> int:
65+
bucket = [0] * 101
66+
for h in heights:
67+
bucket[h] += 1
68+
res = i = 0
69+
for j in range(1, 101):
70+
while bucket[j] > 0:
71+
bucket[j] -= 1
72+
if heights[i] != j:
73+
res += 1
74+
i += 1
75+
return res
76+
```
6477

78+
```python
79+
class Solution:
80+
def heightChecker(self, heights: List[int]) -> int:
81+
expected = sorted(heights)
82+
return sum(1 for i, h in enumerate(heights) if h != expected[i])
6583
```
6684

6785
### **Java**
6886

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

7189
```java
90+
class Solution {
91+
public int heightChecker(int[] heights) {
92+
int[] expected = Arrays.copyOf(heights, heights.length);
93+
Arrays.sort(expected);
94+
int res = 0;
95+
for (int i = 0; i < heights.length; ++i) {
96+
if (heights[i] != expected[i]) {
97+
++res;
98+
}
99+
}
100+
return res;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int heightChecker(vector<int>& heights) {
111+
vector<int> expected = heights;
112+
sort(expected.begin(), expected.end());
113+
int res = 0;
114+
for (int i = 0; i < heights.size(); ++i)
115+
{
116+
if (heights[i] != expected[i]) ++res;
117+
}
118+
return res;
119+
}
120+
};
121+
```
72122
123+
### **Go**
124+
125+
```go
126+
func heightChecker(heights []int) int {
127+
expected := make([]int, len(heights))
128+
copy(expected, heights)
129+
sort.Ints(expected)
130+
res := 0
131+
for i, h := range heights {
132+
if h != expected[i] {
133+
res++
134+
}
135+
}
136+
return res
137+
}
73138
```
74139

75140
### **...**

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,86 @@ All indices match.
5252
<li><code>1 &lt;= heights[i] &lt;= 100</code></li>
5353
</ul>
5454

55-
5655
## Solutions
5756

5857
<!-- tabs:start -->
5958

6059
### **Python3**
6160

6261
```python
62+
class Solution:
63+
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
75+
```
6376

77+
```python
78+
class Solution:
79+
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])
6482
```
6583

6684
### **Java**
6785

6886
```java
87+
class Solution {
88+
public int heightChecker(int[] heights) {
89+
int[] expected = Arrays.copyOf(heights, heights.length);
90+
Arrays.sort(expected);
91+
int res = 0;
92+
for (int i = 0; i < heights.length; ++i) {
93+
if (heights[i] != expected[i]) {
94+
++res;
95+
}
96+
}
97+
return res;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int heightChecker(vector<int>& heights) {
108+
vector<int> expected = heights;
109+
sort(expected.begin(), expected.end());
110+
int res = 0;
111+
for (int i = 0; i < heights.size(); ++i)
112+
{
113+
if (heights[i] != expected[i]) ++res;
114+
}
115+
return res;
116+
}
117+
};
118+
```
69119
120+
### **Go**
121+
122+
```go
123+
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
134+
}
70135
```
71136

72137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
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;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func heightChecker(heights []int) int {
2+
expected := make([]int, len(heights))
3+
copy(expected, heights)
4+
sort.Ints(expected)
5+
res := 0
6+
for i, h := range heights {
7+
if h != expected[i] {
8+
res++
9+
}
10+
}
11+
return res
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int heightChecker(int[] heights) {
3-
int[] copy = Arrays.copyOf(heights, heights.length);
4-
Arrays.sort(copy);
3+
int[] expected = Arrays.copyOf(heights, heights.length);
4+
Arrays.sort(expected);
55
int res = 0;
66
for (int i = 0; i < heights.length; ++i) {
7-
if (heights[i] != copy[i]) {
7+
if (heights[i] != expected[i]) {
88
++res;
99
}
1010
}
1111
return res;
1212
}
13-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def heightChecker(self, heights: List[int]) -> int:
3+
bucket = [0] * 101
4+
for h in heights:
5+
bucket[h] += 1
6+
res = i = 0
7+
for j in range(1, 101):
8+
while bucket[j] > 0:
9+
bucket[j] -= 1
10+
if heights[i] != j:
11+
res += 1
12+
i += 1
13+
return res

0 commit comments

Comments
 (0)