Skip to content

Add solution for 1033 #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1033.java
Original file line number Diff line number Diff line change
@@ -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};
}
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/fishercoder/_1033Test.java
Original file line number Diff line number Diff line change
@@ -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));
}
}