forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1033.java
39 lines (32 loc) · 1.01 KB
/
_1033.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.fishercoder.solutions;
import java.util.Arrays;
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};
}
}
}