-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution to "Minimum Number of Operations to Move All Balls to Each Box"
problem
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...om/github/dkoval/leetcode/challenge/MinimumNumberOfOperationsToMoveAllBallsToEachBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../github/dkoval/leetcode/challenge/MinimumNumberOfOperationsToMoveAllBallsToEachBoxTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |