Skip to content

Commit d7cbf26

Browse files
authored
feat: add ts solution to lc problem: No.1051 (doocs#3087)
1 parent fbd3df3 commit d7cbf26

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ func heightChecker(heights []int) int {
142142
}
143143
```
144144

145+
#### TypeScript
146+
147+
```ts
148+
function heightChecker(heights: number[]): number {
149+
const expected = [...heights].sort((a, b) => a - b);
150+
return heights.reduce((acc, cur, i) => acc + (cur !== expected[i] ? 1 : 0), 0);
151+
}
152+
```
153+
145154
<!-- tabs:end -->
146155

147156
<!-- solution:end -->
@@ -239,6 +248,25 @@ func heightChecker(heights []int) int {
239248
}
240249
```
241250

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

244272
<!-- solution:end -->

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ func heightChecker(heights []int) int {
137137
}
138138
```
139139

140+
#### TypeScript
141+
142+
```ts
143+
function heightChecker(heights: number[]): number {
144+
const expected = [...heights].sort((a, b) => a - b);
145+
return heights.reduce((acc, cur, i) => acc + (cur !== expected[i] ? 1 : 0), 0);
146+
}
147+
```
148+
140149
<!-- tabs:end -->
141150

142151
<!-- solution:end -->
@@ -230,6 +239,25 @@ func heightChecker(heights []int) int {
230239
}
231240
```
232241

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

235263
<!-- solution:end -->
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function heightChecker(heights: number[]): number {
2+
const expected = [...heights].sort((a, b) => a - b);
3+
return heights.reduce((acc, cur, i) => acc + (cur !== expected[i] ? 1 : 0), 0);
4+
}
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)