Skip to content

Commit 66934b2

Browse files
committed
feat: add 2nd ts solution to lc problem: No.1051
1 parent 0683588 commit 66934b2

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func heightChecker(heights []int) int {
144144

145145
#### TypeScript
146146

147-
````ts
147+
```ts
148148
function heightChecker(heights: number[]): number {
149149
const expected = [...heights].sort((a, b) => a - b);
150150
let ans = 0;
@@ -154,7 +154,8 @@ function heightChecker(heights: number[]): number {
154154
}
155155

156156
return ans;
157-
}```
157+
}
158+
```
158159

159160
<!-- tabs:end -->
160161

@@ -186,7 +187,7 @@ class Solution:
186187
ans += 1
187188
i += 1
188189
return ans
189-
````
190+
```
190191

191192
#### Java
192193

@@ -253,6 +254,25 @@ func heightChecker(heights []int) int {
253254
}
254255
```
255256

257+
#### TypeScript
258+
259+
```ts
260+
function heightChecker(heights: number[]): number {
261+
const cnt = Array(101).fill(0);
262+
for (const i of heights) cnt[i]++;
263+
264+
let ans = 0;
265+
for (let j = 1, i = 0; j < 101; j++) {
266+
while (cnt[j]--)
267+
if (heights[i++] !== j) {
268+
ans++;
269+
}
270+
}
271+
272+
return ans;
273+
}
274+
```
275+
256276
<!-- tabs:end -->
257277

258278
<!-- solution:end -->

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func heightChecker(heights []int) int {
139139

140140
#### TypeScript
141141

142-
````ts
142+
```ts
143143
function heightChecker(heights: number[]): number {
144144
const expected = [...heights].sort((a, b) => a - b);
145145
let ans = 0;
@@ -149,7 +149,8 @@ function heightChecker(heights: number[]): number {
149149
}
150150

151151
return ans;
152-
}```
152+
}
153+
```
153154

154155
<!-- tabs:end -->
155156

@@ -177,7 +178,7 @@ class Solution:
177178
ans += 1
178179
i += 1
179180
return ans
180-
````
181+
```
181182

182183
#### Java
183184

@@ -244,6 +245,25 @@ func heightChecker(heights []int) int {
244245
}
245246
```
246247

248+
#### TypeScript
249+
250+
```ts
251+
function heightChecker(heights: number[]): number {
252+
const cnt = Array(101).fill(0);
253+
for (const i of heights) cnt[i]++;
254+
255+
let ans = 0;
256+
for (let j = 1, i = 0; j < 101; j++) {
257+
while (cnt[j]--)
258+
if (heights[i++] !== j) {
259+
ans++;
260+
}
261+
}
262+
263+
return ans;
264+
}
265+
```
266+
247267
<!-- tabs:end -->
248268

249269
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function heightChecker(heights: number[]): number {
2+
const cnt = Array(101).fill(0);
3+
for (const i of heights) cnt[i]++;
4+
5+
let ans = 0;
6+
for (let j = 1, i = 0; j < 101; j++) {
7+
while (cnt[j]--)
8+
if (heights[i++] !== j) {
9+
ans++;
10+
}
11+
}
12+
13+
return ans;
14+
}

0 commit comments

Comments
 (0)