File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 513
513
1764|[ Form Array by Concatenating Subarrays of Another Array] ( ./1764-form-array-by-concatenating-subarrays-of-another-array.js ) |Medium|
514
514
1765|[ Map of Highest Peak] ( ./1765-map-of-highest-peak.js ) |Medium|
515
515
1768|[ Merge Strings Alternately] ( ./1768-merge-strings-alternately.js ) |Easy|
516
+ 1769|[ Minimum Number of Operations to Move All Balls to Each Box] ( ./1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js ) |Medium|
516
517
1780|[ Check if Number is a Sum of Powers of Three] ( ./1780-check-if-number-is-a-sum-of-powers-of-three.js ) |Medium|
517
518
1790|[ Check if One String Swap Can Make Strings Equal] ( ./1790-check-if-one-string-swap-can-make-strings-equal.js ) |Easy|
518
519
1791|[ Find Center of Star Graph] ( ./1791-find-center-of-star-graph.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1769. Minimum Number of Operations to Move All Balls to Each Box
3
+ * https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
4
+ * Difficulty: Medium
5
+ *
6
+ * You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if
7
+ * the ith box is empty, and '1' if it contains one ball.
8
+ *
9
+ * In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to
10
+ * box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some
11
+ * boxes.
12
+ *
13
+ * Return an array answer of size n, where answer[i] is the minimum number of operations needed
14
+ * to move all the balls to the ith box.
15
+ *
16
+ * Each answer[i] is calculated considering the initial state of the boxes.
17
+ */
18
+
19
+ /**
20
+ * @param {string } boxes
21
+ * @return {number[] }
22
+ */
23
+ var minOperations = function ( boxes ) {
24
+ const result = new Array ( boxes . length ) . fill ( 0 ) ;
25
+
26
+ for ( let i = 0 ; i < boxes . length ; i ++ ) {
27
+ let total = 0 ;
28
+ for ( let j = 0 ; j < boxes . length ; j ++ ) {
29
+ if ( boxes [ j ] === '1' ) {
30
+ total += Math . abs ( j - i ) ;
31
+ }
32
+ }
33
+ result [ i ] = total ;
34
+ }
35
+
36
+ return result ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments