-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path739.php
84 lines (76 loc) · 2.06 KB
/
739.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
<?php
/**
* Class Solution
* 739.根据每日 气温 列表,请重新生成一个列表,对应位置的输出是需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。
* @link https://leetcode-cn.com/problems/daily-temperatures/
*/
class Solution
{
/**
* 暴力破解,超时未通过
* @param Integer[] $T
* @return Integer[]
*/
function dailyTemperatures($T)
{
$res = [];
$l = count($T);
foreach ($T as $k => $v) {
$i = $k + 1;
$is_true = false;
while ($i < $l) {
if ($T[$i++] > $T[$k]) {
$is_true = true;
break;
}
}
$res[] = !$is_true ? 0 : $i - $k - 1;
}
return $res;
}
/**
* 数组模拟栈,依旧超时
* @param $T
* @return array
*/
public function dailyTemperatures1($T)
{
$length = count($T);
$res = array_fill(0, $length, 0);
$st = [];
for ($i = 0; $i < $length; $i++) {
while (!empty($st) && ($T[$i] > $T[current($st)])) {
$t = array_shift($st);
$res[$t] = $i - $t;
}
array_unshift($st, $i);
}
return $res;
}
/**
* PHP单调栈,未超时
* @param $T
* @return array
*/
public function dailyTemperatures2($T)
{
$n = count($T);
$ans = array_fill(0, $n, 0);
if ($n == 0) return $ans;
$stack = new SplStack();
for ($i = 0; $i < $n; ++$i) {
while (!$stack->isEmpty() && $T[$stack->top()] < $T[$i]) {
// 出栈
$top = $stack->pop();
$ans[$top] = $i - $top;
}
$stack->push($i);
}
return $ans;
}
}
$obj = new Solution();
$temperatures = [73, 74, 75, 71, 69, 72, 76, 73];
//$temperatures = [55,38,53,81,61,93,97,32,43,78];
$res = $obj->dailyTemperatures1($temperatures);
print_r($res);