-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path946.php
More file actions
38 lines (36 loc) · 980 Bytes
/
Copy path946.php
File metadata and controls
38 lines (36 loc) · 980 Bytes
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
<?php
class Solution
{
function validateStackSequences($pushed, $popped)
{
$pivot = 0;
$len = count($pushed);
$ans = [];
for ($i = 0; $i < $len; $i++) {
if ($pushed[$i] == $popped[$pivot]) {
$pivot += 1;
while (count($ans) > 0 && $ans[array_key_last($ans)] == $popped[$pivot]) {
array_pop($ans);
$pivot += 1;
}
} else {
array_push($ans, $pushed[$i]);
}
}
for($i=$pivot;$i<count($popped);$i++){
if ($popped[$i]!=array_pop($ans)) {
return false;
}
}
return count($ans)>0?false:true;
}
}
$obj = new Solution();
// $pushed = [1, 2, 3, 4, 5];
// $popped =[4, 5, 3, 2, 1];
// $popped= [4, 3, 5, 1, 2];
$pushed = [2,1,0];
$popped = [ 1, 2,0];
echo "<pre>";
print_r($obj->validateStackSequences($pushed, $popped));
echo "</pre>";