Skip to content

Commit 5421d06

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 1033 (fishercoder1534#61)
1 parent d4bcb9b commit 5421d06

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|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|
3031
|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|
3132
|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|
3233
|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|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1033. Moving Stones Until Consecutive
7+
*
8+
* Three stones are on a number line at positions a, b, and c.
9+
*
10+
* Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone),
11+
* and move it to an unoccupied position between those endpoints.
12+
* Formally, let's say the stones are currently at positions x, y, z with x < y < z.
13+
* 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.
14+
*
15+
* The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.
16+
*
17+
* When the game ends, what is the minimum and maximum number of moves that you could have made?
18+
* Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]
19+
*
20+
* Note:
21+
* * 1 <= a <= 100
22+
* * 1 <= b <= 100
23+
* * 1 <= c <= 100
24+
* * a != b, b != c, c != a
25+
*/
26+
27+
public class _1033 {
28+
public static class Solution1 {
29+
private int minMoves(int x, int y, int z) {
30+
// already consecutive integers, nothing to be done
31+
if (x + 1 == y && y + 1 == z) {
32+
return 0;
33+
}
34+
// one of the following (sample) cases:
35+
// 1, 2, 8 (8 -> 3)
36+
// 1, 7, 8 (1 -> 6)
37+
// 1, 3, 8 (8 -> 2)
38+
// 1, 6, 8 (1 -> 7)
39+
if (y - x <= 2 || z - y <= 2) {
40+
return 1;
41+
}
42+
43+
// move z to y + 1, x to y - 1
44+
return 2;
45+
}
46+
47+
private int maxMoves(int x, int y, int z) {
48+
return z - x - 2;
49+
}
50+
51+
public int[] numMovesStones(int a, int b, int c) {
52+
int[] t = {a, b, c};
53+
Arrays.sort(t);
54+
55+
int min = minMoves(t[0], t[1], t[2]);
56+
int max = maxMoves(t[0], t[1], t[2]);
57+
58+
return new int[]{min, max};
59+
}
60+
}
61+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1033;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1033Test {
10+
private static _1033.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1033.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[] expected = {1, 2};
20+
assertArrayEquals(expected, solution1.numMovesStones(1, 2, 5));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
int[] expected = {0, 0};
26+
assertArrayEquals(expected, solution1.numMovesStones(4, 3, 2));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
int[] expected = {1, 2};
32+
assertArrayEquals(expected, solution1.numMovesStones(3, 5, 1));
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy