File tree Expand file tree Collapse file tree 3 files changed +56
-3
lines changed
solution/1000-1099/1051.Height Checker Expand file tree Collapse file tree 3 files changed +56
-3
lines changed Original file line number Diff line number Diff line change @@ -253,6 +253,25 @@ func heightChecker(heights []int) int {
253
253
}
254
254
` ` `
255
255
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
+
256
275
< ! -- tabs :end -- >
257
276
258
277
< ! -- solution :end -- >
Original file line number Diff line number Diff line change @@ -139,7 +139,7 @@ func heightChecker(heights []int) int {
139
139
140
140
#### TypeScript
141
141
142
- ```` ts
142
+ ``` ts
143
143
function heightChecker(heights : number []): number {
144
144
const expected = [... heights ].sort ((a , b ) => a - b );
145
145
let ans = 0 ;
@@ -149,7 +149,8 @@ function heightChecker(heights: number[]): number {
149
149
}
150
150
151
151
return ans ;
152
- }` ` `
152
+ }
153
+ ```
153
154
154
155
<!-- tabs: end -->
155
156
@@ -177,7 +178,7 @@ class Solution:
177
178
ans += 1
178
179
i += 1
179
180
return ans
180
- ````
181
+ ```
181
182
182
183
#### Java
183
184
@@ -244,6 +245,25 @@ func heightChecker(heights []int) int {
244
245
}
245
246
```
246
247
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
+
247
267
<!-- tabs: end -->
248
268
249
269
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments