-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1041-robot-bounded-in-circle.php
155 lines (141 loc) · 3.86 KB
/
1041-robot-bounded-in-circle.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
<?php
// 1041. Robot Bounded In Circle
// https://leetcode.com/problems/robot-bounded-in-circle/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 4 ms, faster than 100.00% of PHP online submissions
// for Robot Bounded In Circle.
// Memory Usage: 14.7 MB, less than 100.00% of PHP online submission
// for Robot Bounded In Circle.
// class Solution
// {
// /**
// * @param String $instructions
// * @return Boolean
// */
// function isRobotBounded($instructions)
// {
// $direction = 0;
// $position = 0;
// for ($i = 0; $i < strlen($instructions); $i++) {
// switch ($instructions[$i]) {
// case 'G':
// $position += [1, 2, -1, -2][$direction];
// break;
// case 'L':
// $direction = ($direction + 1) % 4;
// break;
// case 'R':
// $direction = ($direction + 3) % 4;
// break;
// }
// }
// return 0 === $position || 0 !== $direction;
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 0 ms, faster than 100.00% of PHP online submissions
// for Robot Bounded In Circle.
// Memory Usage: 14.9 MB, less than 100.00% of PHP online submissions
// for Robot Bounded In Circle.
class Solution
{
/**
* @param String $instructions
* @return Boolean
*/
function isRobotBounded($instructions)
{
list($distance, $orientation) = [0, 0];
for ($i = 0; $i < strlen($instructions); $i++) {
switch ($instructions[$i]) {
case 'G':
$distance += [1, 2, -1, -2][$orientation];
break;
case 'L':
$orientation = ($orientation + 1) % 4;
break;
case 'R':
$orientation = ($orientation + 3) % 4;
break;
}
}
return 0 === $distance || 0 !== $orientation;
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$tests = [
[
'name' => 'GGLLGG',
'input' => 'GGLLGG',
'expected' => true,
],
[
'name' => 'GG',
'input' => 'GG',
'expected' => false,
],
[
'name' => 'GL',
'input' => 'GL',
'expected' => true,
],
[
'name' => 'LLL',
'input' => 'LLL',
'expected' => true,
],
[
'name' => 'LLGRL',
'input' => 'LLGRL',
'expected' => true,
],
[
'name' => 'GLGLGGLGL',
'input' => 'GLGLGGLGL',
'expected' => false,
],
[
'name' => 'LLLRLLLRLLGLLGGRGLLLGGLRRRRRGLRLRLRLGGRGRGRLLLLLLGLLRLGLGLRLGGGRR',
'input' => 'LLLRLLLRLLGLLGGRGLLLGGLRRRRRGLRLRLRLGGRGRGRLLLLLLGLLRLGLGLRLGGGRR',
'expected' => false,
],
[
'name' => 'RRLRRRRRRRRLRRRLLRLL',
'input' => 'RRLRRRRRRRRLRRRLLRLL',
'expected' => true,
],
[
'name' => 'LRG',
'input' => 'LRG',
'expected' => false,
],
// ➡➡⬇⬇⬇⬅⬅⬅⬆⬆⬆➡
[
'name' => 'GGRGGRGGRGGR',
'input' => 'GGRGGRGGRGGR',
'expected' => true,
],
// ➡⬆➡⬆⬅⬅⬇
[
'name' => 'GLRLLGLL',
'input' => 'GLRLLGLL',
'expected' => true,
],
[
'name' => 'GRGL',
'input' => 'GRGL',
'expected' => false,
]
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
$output = $s->isRobotBounded($input);
if ($expected === $output) {
echo "✅ {$name}<br>";
} else {
echo "🔴 {$name}<br>";
echo "Expected \"{$expected}\", but got \"{$output}\"<br>";
}
echo '<hr>';
}