Skip to content

Commit 5342d49

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

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ 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 -->

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)