-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMUS.php
66 lines (63 loc) · 1.6 KB
/
MUS.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
<?php
class solution
{
function mus($a)
{
$start = 0;
$end = count($a);
$n = $end;
for ($i = 1; $i < $n; $i++) {
if ($a[$i] < $a[$i - 1]) {
$start = $i - 1;
break;
}
}
for ($i = $n - 1; $i > 0; $i--) {
if ($a[$i] < $a[$i - 1]) {
$end = $i;
break;
}
}
if (($end - $start) == $n) {
return -1;
} else {
$f = -10;
for ($i = $end; $i < $n; $i++) { //last
for ($j = 0; $j < $start; $j++) { //first
if ($a[$j] > $a[$i]) {
$nstart = $j;
$f = 0;
break;
}
}
if ($f == 0) {
break;
}
}
$x = -1;
for ($i = 0; $i <= $start; $i++) { // first
for ($j = $n - 1; $j >= $end; $j--) { // last
if ($a[$i] > $a[$j]) {
$nend = $j;
$x = 0;
break;
}
}
if ($x == 0) {
break;
}
}
// while ($start >= 0 && $end <= $n - 1) {
// if ($a[$start]>$a[$end]) {
// }
// }
return [$nstart, $nend];
}
}
}
$obj = new solution();
// $a = [5, 6, 1, 2, 4, 7];
$a = [3, 2, 1];
echo "<pre>";
print_r($obj->mus($a));
echo "</pre>";