Skip to content

Commit 1aea00e

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

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,28 @@ func heightChecker(heights []int) int {
253253
}
254254
```
255255

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

258277
<!-- solution:end -->
259278

260279
<!-- problem:end -->
280+
````

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,28 @@ func heightChecker(heights []int) int {
244244
}
245245
```
246246

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

249268
<!-- solution:end -->
250269

251270
<!-- problem:end -->
271+
````
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)