diff --git a/README.md b/README.md index 49a0a23938..ee389b5a6b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome! | # | Title | Solutions | Time | Space | Video | Difficulty | Tag |-----|----------------|---------------|---------------|---------------|--------|-------------|------------- +|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | O(1) | O(1) | |Easy|Math| |1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy| |1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | O(n) | O(n) | |Easy| |1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy| diff --git a/src/main/java/com/fishercoder/solutions/_1033.java b/src/main/java/com/fishercoder/solutions/_1033.java new file mode 100644 index 0000000000..3c4759ae48 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1033.java @@ -0,0 +1,61 @@ +package com.fishercoder.solutions; + +import java.util.Arrays; + +/** + * 1033. Moving Stones Until Consecutive + * + * Three stones are on a number line at positions a, b, and c. + * + * Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone), + * and move it to an unoccupied position between those endpoints. + * Formally, let's say the stones are currently at positions x, y, z with x < y < z. + * You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y. + * + * The game ends when you cannot make any more moves, ie. the stones are in consecutive positions. + * + * When the game ends, what is the minimum and maximum number of moves that you could have made? + * Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves] + * + * Note: + * * 1 <= a <= 100 + * * 1 <= b <= 100 + * * 1 <= c <= 100 + * * a != b, b != c, c != a + */ + +public class _1033 { + public static class Solution1 { + private int minMoves(int x, int y, int z) { + // already consecutive integers, nothing to be done + if (x + 1 == y && y + 1 == z) { + return 0; + } + // one of the following (sample) cases: + // 1, 2, 8 (8 -> 3) + // 1, 7, 8 (1 -> 6) + // 1, 3, 8 (8 -> 2) + // 1, 6, 8 (1 -> 7) + if (y - x <= 2 || z - y <= 2) { + return 1; + } + + // move z to y + 1, x to y - 1 + return 2; + } + + private int maxMoves(int x, int y, int z) { + return z - x - 2; + } + + public int[] numMovesStones(int a, int b, int c) { + int[] t = {a, b, c}; + Arrays.sort(t); + + int min = minMoves(t[0], t[1], t[2]); + int max = maxMoves(t[0], t[1], t[2]); + + return new int[]{min, max}; + } + } +} diff --git a/src/test/java/com/fishercoder/_1033Test.java b/src/test/java/com/fishercoder/_1033Test.java new file mode 100644 index 0000000000..6d2bc09028 --- /dev/null +++ b/src/test/java/com/fishercoder/_1033Test.java @@ -0,0 +1,34 @@ +package com.fishercoder; + +import com.fishercoder.solutions._1033; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; + +public class _1033Test { + private static _1033.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _1033.Solution1(); + } + + @Test + public void test1() { + int[] expected = {1, 2}; + assertArrayEquals(expected, solution1.numMovesStones(1, 2, 5)); + } + + @Test + public void test2() { + int[] expected = {0, 0}; + assertArrayEquals(expected, solution1.numMovesStones(4, 3, 2)); + } + + @Test + public void test3() { + int[] expected = {1, 2}; + assertArrayEquals(expected, solution1.numMovesStones(3, 5, 1)); + } +} \ No newline at end of file