File tree Expand file tree Collapse file tree 3 files changed +60
-6
lines changed
solution/1000-1099/1051.Height Checker Expand file tree Collapse file tree 3 files changed +60
-6
lines changed Original file line number Diff line number Diff line change @@ -144,7 +144,7 @@ func heightChecker(heights []int) int {
144
144
145
145
#### TypeScript
146
146
147
- ```` ts
147
+ ``` ts
148
148
function heightChecker(heights : number []): number {
149
149
const expected = [... heights ].sort ((a , b ) => a - b );
150
150
let ans = 0 ;
@@ -154,7 +154,8 @@ function heightChecker(heights: number[]): number {
154
154
}
155
155
156
156
return ans ;
157
- }` ` `
157
+ }
158
+ ```
158
159
159
160
<!-- tabs: end -->
160
161
@@ -186,7 +187,7 @@ class Solution:
186
187
ans += 1
187
188
i += 1
188
189
return ans
189
- ````
190
+ ```
190
191
191
192
#### Java
192
193
@@ -253,6 +254,25 @@ func heightChecker(heights []int) int {
253
254
}
254
255
```
255
256
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
+
256
276
<!-- tabs: end -->
257
277
258
278
<!-- 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