File tree Expand file tree Collapse file tree 4 files changed +154
-0
lines changed
solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box Expand file tree Collapse file tree 4 files changed +154
-0
lines changed Original file line number Diff line number Diff line change @@ -447,4 +447,62 @@ int* minOperations(char* boxes, int* returnSize) {
447447
448448<!-- solution:end -->
449449
450+ <!-- solution:start -->
451+
452+ ### Solution 3
453+
454+ <!-- tabs:start -->
455+
456+ #### TypeScript
457+
458+ ```ts
459+ function minOperations(boxes: string): number[] {
460+ const n = boxes.length;
461+ const ans = Array(n).fill(0);
462+ const ones: number[] = [];
463+
464+ for (let i = 0; i < n; i++) {
465+ if (+boxes[i]) {
466+ ones.push(i);
467+ }
468+ }
469+
470+ for (let i = 0; i < n; i++) {
471+ for (const j of ones) {
472+ ans[i] += Math.abs(i - j);
473+ }
474+ }
475+
476+ return ans;
477+ }
478+ ```
479+
480+ #### JavaScript
481+
482+ ``` js
483+ function minOperations (boxes ) {
484+ const n = boxes .length ;
485+ const ans = Array (n).fill (0 );
486+ const ones = [];
487+
488+ for (let i = 0 ; i < n; i++ ) {
489+ if (+ boxes[i]) {
490+ ones .push (i);
491+ }
492+ }
493+
494+ for (let i = 0 ; i < n; i++ ) {
495+ for (const j of ones) {
496+ ans[i] += Math .abs (i - j);
497+ }
498+ }
499+
500+ return ans;
501+ }
502+ ```
503+
504+ <!-- tabs:end -->
505+
506+ <!-- solution:end -->
507+
450508<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -439,4 +439,62 @@ int* minOperations(char* boxes, int* returnSize) {
439439
440440<!-- solution:end -->
441441
442+ <!-- solution:start -->
443+
444+ ### Solution 3
445+
446+ <!-- tabs:start -->
447+
448+ #### TypeScript
449+
450+ ```ts
451+ function minOperations(boxes: string): number[] {
452+ const n = boxes.length;
453+ const ans = Array(n).fill(0);
454+ const ones: number[] = [];
455+
456+ for (let i = 0; i < n; i++) {
457+ if (+boxes[i]) {
458+ ones.push(i);
459+ }
460+ }
461+
462+ for (let i = 0; i < n; i++) {
463+ for (const j of ones) {
464+ ans[i] += Math.abs(i - j);
465+ }
466+ }
467+
468+ return ans;
469+ }
470+ ```
471+
472+ #### JavaScript
473+
474+ ``` js
475+ function minOperations (boxes ) {
476+ const n = boxes .length ;
477+ const ans = Array (n).fill (0 );
478+ const ones = [];
479+
480+ for (let i = 0 ; i < n; i++ ) {
481+ if (+ boxes[i]) {
482+ ones .push (i);
483+ }
484+ }
485+
486+ for (let i = 0 ; i < n; i++ ) {
487+ for (const j of ones) {
488+ ans[i] += Math .abs (i - j);
489+ }
490+ }
491+
492+ return ans;
493+ }
494+ ```
495+
496+ <!-- tabs:end -->
497+
498+ <!-- solution:end -->
499+
442500<!-- problem:end -->
Original file line number Diff line number Diff line change 1+ function minOperations ( boxes ) {
2+ const n = boxes . length ;
3+ const ans = Array ( n ) . fill ( 0 ) ;
4+ const ones = [ ] ;
5+
6+ for ( let i = 0 ; i < n ; i ++ ) {
7+ if ( + boxes [ i ] ) {
8+ ones . push ( i ) ;
9+ }
10+ }
11+
12+ for ( let i = 0 ; i < n ; i ++ ) {
13+ for ( const j of ones ) {
14+ ans [ i ] += Math . abs ( i - j ) ;
15+ }
16+ }
17+
18+ return ans ;
19+ }
Original file line number Diff line number Diff line change 1+ function minOperations ( boxes : string ) : number [ ] {
2+ const n = boxes . length ;
3+ const ans = Array ( n ) . fill ( 0 ) ;
4+ const ones : number [ ] = [ ] ;
5+
6+ for ( let i = 0 ; i < n ; i ++ ) {
7+ if ( + boxes [ i ] ) {
8+ ones . push ( i ) ;
9+ }
10+ }
11+
12+ for ( let i = 0 ; i < n ; i ++ ) {
13+ for ( const j of ones ) {
14+ ans [ i ] += Math . abs ( i - j ) ;
15+ }
16+ }
17+
18+ return ans ;
19+ }
You can’t perform that action at this time.
0 commit comments