File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed
solution/3000-3099/3016.Minimum Number of Pushes to Type Word II Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -210,4 +210,39 @@ function minimumPushes(word) {
210
210
211
211
<!-- solution: end -->
212
212
213
+ <!-- solution: start -->
214
+
215
+ ### Solution 2: Priority Queue
216
+
217
+ <!-- tabs: start -->
218
+
219
+ #### TypeScript
220
+
221
+ ``` ts
222
+ function minimumPushes(word : string ): number {
223
+ const pq = new MaxPriorityQueue ();
224
+ const cnt = new Map <string , number >();
225
+ let [i, res] = [0 , 0 ];
226
+
227
+ for (const x of word ) {
228
+ cnt .set (x , (cnt .get (x ) ?? 0 ) + 1 );
229
+ }
230
+
231
+ for (const [x, c] of cnt ) {
232
+ pq .enqueue (x , c );
233
+ }
234
+
235
+ while (! pq .isEmpty ()) {
236
+ const c = pq .dequeue ().priority ;
237
+ res += c * (((i ++ / 8 ) | 0 ) + 1 );
238
+ }
239
+
240
+ return res ;
241
+ }
242
+ ```
243
+
244
+ <!-- tabs: end -->
245
+
246
+ <!-- solution: end -->
247
+
213
248
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -208,4 +208,39 @@ function minimumPushes(word) {
208
208
209
209
<!-- solution: end -->
210
210
211
+ <!-- solution: start -->
212
+
213
+ ### Solution 2: Priority Queue
214
+
215
+ <!-- tabs: start -->
216
+
217
+ #### TypeScript
218
+
219
+ ``` ts
220
+ function minimumPushes(word : string ): number {
221
+ const pq = new MaxPriorityQueue ();
222
+ const cnt = new Map <string , number >();
223
+ let [i, res] = [0 , 0 ];
224
+
225
+ for (const x of word ) {
226
+ cnt .set (x , (cnt .get (x ) ?? 0 ) + 1 );
227
+ }
228
+
229
+ for (const [x, c] of cnt ) {
230
+ pq .enqueue (x , c );
231
+ }
232
+
233
+ while (! pq .isEmpty ()) {
234
+ const c = pq .dequeue ().priority ;
235
+ res += c * (((i ++ / 8 ) | 0 ) + 1 );
236
+ }
237
+
238
+ return res ;
239
+ }
240
+ ```
241
+
242
+ <!-- tabs: end -->
243
+
244
+ <!-- solution: end -->
245
+
211
246
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function minimumPushes ( word : string ) : number {
2
+ const pq = new MaxPriorityQueue ( ) ;
3
+ const cnt = new Map < string , number > ( ) ;
4
+ let [ i , res ] = [ 0 , 0 ] ;
5
+
6
+ for ( const x of word ) {
7
+ cnt . set ( x , ( cnt . get ( x ) ?? 0 ) + 1 ) ;
8
+ }
9
+
10
+ for ( const [ x , c ] of cnt ) {
11
+ pq . enqueue ( x , c ) ;
12
+ }
13
+
14
+ while ( ! pq . isEmpty ( ) ) {
15
+ const c = pq . dequeue ( ) . priority ;
16
+ res += c * ( ( ( i ++ / 8 ) | 0 ) + 1 ) ;
17
+ }
18
+
19
+ return res ;
20
+ }
You can’t perform that action at this time.
0 commit comments