-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1033-moving-stones-until-consecutive.php
156 lines (140 loc) · 4.55 KB
/
1033-moving-stones-until-consecutive.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// 1033. Moving Stones Until Consecutive
// https://leetcode.com/problems/moving-stones-until-consecutive/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 0 ms, faster than 100.00% of PHP online submissions
// for Moving Stones Until Consecutive.
// Memory Usage: 14.9 MB, less than 100.00% of PHP online submissions
// for Moving Stones Until Consecutive.
// class Solution
// {
// /**
// * @param Integer $a
// * @param Integer $b
// * @param Integer $c
// * @return Integer[]
// */
// function numMovesStones($a, $b, $c)
// {
// $arr = array($a, $b, $c);
// sort($arr);
// list($min, $mid, $max) = $arr;
// $maxMoves = $max - $min - 2;
// $minMoves = 0;
// if (2 === $mid - $min) {
// $max = $mid;
// $mid--;
// $minMoves++;
// }
// if (2 === $max - $mid) {
// $min = $mid;
// $mid++;
// $minMoves++;
// }
// if (1 < $mid - $min) $minMoves++;
// if (1 < $max - $mid) $minMoves++;
// return [$minMoves, $maxMoves];
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 8 ms, faster than 100.00% of PHP online submissions
// for Moving Stones Until Consecutive.
// Memory Usage: 14.9 MB, less than 100.00% of PHP online submissions
// for Moving Stones Until Consecutive.
// Get the distances between the stones to avoid sorting
class Solution
{
/**
* @param Integer $a
* @param Integer $b
* @param Integer $c
* @return Integer[]
*/
function numMovesStones($a, $b, $c)
{
list($x, $y, $z) = [abs($a - $b), abs($b - $c), abs($c - $a)];
list($min, $max) = [min($x, $y, $z), max($x, $y, $z)];
if (2 === $max) return [0, 0];
return [$min < 3 ? 1 : 2, $max - 2];
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$tests = [
[
'name' => 'a=1, b=2, c=5]',
'input' => ['a' => 1, 'b' => 2, 'c' => 5],
'expected' => [1, 2],
// Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
// | 1 | 2 | 3 | 4 | 5 |
// init | a | b | - | - | c |
// min 1 | a | b | c | - | - |
// max 1 | a | b | - | c | - |
// max 2 | a | b | c | - | - |
],
[
'name' => 'a=4, b=3, c=2',
'input' => ['a' => 4, 'b' => 3, 'c' => 2],
'expected' => [0, 0],
// Explanation: We cannot make any moves.
// | 1 | 2 | 3 | 4 |
// init | - | c | b | a |
],
[
'name' => 'a=3, b=5, c=1',
'input' => ['a' => 3, 'b' => 5, 'c' => 1],
'expected' => [1, 2],
// Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
// | 1 | 2 | 3 | 4 | 5 |
// init | c | - | a | - | b |
// min 1 | - | - | a | c | b |
// max 1 | - | c | a | - | b |
// max 2 | - | c | a | b | - |
],
[
'name' => 'a=1, b=3, c=6',
'input' => ['a' => 1, 'b' => 3, 'c' => 6],
'expected' => [1, 3],
// | 1 | 2 | 3 | 4 | 5 | 6 |
// init | a | - | b | - | - | c |
// min 1 | a | c | b | - | - | - |
// max 1 | - | a | b | - | - | c |
// max 2 | - | a | b | - | c | - |
// max 3 | - | a | b | c | - | - |
],
[
'name' => 'a=2, b=4, c=1',
'input' => ['a' => 2, 'b' => 4, 'c' => 1],
'expected' => [1, 1],
// | 1 | 2 | 3 | 4 |
// init | c | a | - | b |
// min 1 | - | a | c | b |
// max 1 | c | a | b | - |
],
[
'name' => 'a=1, b=4, c=5',
'input' => ['a' => 1, 'b' => 4, 'c' => 5],
'expected' => [1, 2],
// Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
// | 1 | 2 | 3 | 4 | 5 |
// init | a | - | - | b | c |
// min 1 | - | - | a | b | c |
// max 1 | - | a | - | b | c |
// max 2 | - | - | a | b | c |
],
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
extract($input);
$output = $s->numMovesStones($a, $b, $c);
if ($expected === $output) {
echo "✅ {$name}<br>";
} else {
echo "🔴 {$name}<br>";
echo 'Expected the below';
echo '<pre>', print_r($expected, true), '</pre>';
echo 'But got the below instead';
echo '<pre>', print_r($output, true), '</pre>';
}
echo '<hr>';
}