Skip to content

Commit

Permalink
Solution to "Minimum Number of Operations to Move All Balls to Each Box"
Browse files Browse the repository at this point in the history
 problem
  • Loading branch information
dkoval committed Jan 6, 2025
1 parent fab5d56 commit ef11d02
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.dkoval.leetcode.challenge;

/**
* <a href="https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/">Minimum Number of Operations to Move All Balls to Each Box</a>
* <p>
* You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty,
* and '1' if it contains one ball.
* <p>
* In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1.
* Note that after doing so, there may be more than one ball in some boxes.
* <p>
* Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls
* to the ith box.
* <p>
* Each answer[i] is calculated considering the initial state of the boxes.
* <p>
* Constraints:
* <ul>
* <li>n == boxes.length</li>
* <li>1 <= n <= 2000</li>
* <li>boxes[i] is either '0' or '1'.</li>
* </ul>
*/
public interface MinimumNumberOfOperationsToMoveAllBallsToEachBox {

int[] minOperations(String boxes);

// O(N^2) time | O(1) space
class MinimumNumberOfOperationsToMoveAllBallsToEachBoxRev1 implements MinimumNumberOfOperationsToMoveAllBallsToEachBox {

@Override
public int[] minOperations(String boxes) {
final var n = boxes.length();

final var ans = new int[n];
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
if (boxes.charAt(j) == '1') {
ans[i] += Math.abs(i - j);
}
}
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.dkoval.leetcode.challenge

import com.github.dkoval.leetcode.challenge.MinimumNumberOfOperationsToMoveAllBallsToEachBox.MinimumNumberOfOperationsToMoveAllBallsToEachBoxRev1
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.util.stream.Stream

internal class MinimumNumberOfOperationsToMoveAllBallsToEachBoxTest {

class InputArgumentsProvider : ArgumentsProvider {

override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of(
Arguments.of(
"110",
intArrayOf(1, 1, 3)
),
Arguments.of(
"001011",
intArrayOf(11, 8, 5, 4, 3, 4)
)
)
}

@Nested
inner class MinimumNumberOfOperationsToMoveAllBallsToEachBoxRev1Test {

@ParameterizedTest
@ArgumentsSource(InputArgumentsProvider::class)
fun `should return an array answer of size n, where i-th answer is the minimum number of operations needed to move all the balls to the ith box`(
boxes: String,
expected: IntArray
) {
MinimumNumberOfOperationsToMoveAllBallsToEachBoxRev1().test(boxes, expected)
}
}
}

private fun MinimumNumberOfOperationsToMoveAllBallsToEachBox.test(boxes: String, expected: IntArray) {
val actual = minOperations(boxes)
assertArrayEquals(expected, actual)
}

0 comments on commit ef11d02

Please sign in to comment.