forked from LeetCode-in-Php/LeetCode-in-Php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.php
32 lines (29 loc) · 975 Bytes
/
Solution.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
<?php
namespace leetcode\g0701_0800\s0739_daily_temperatures;
// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #Programming_Skills_II_Day_6
// #Big_O_Time_O(n)_Space_O(n) #2023_12_24_Time_410_ms_(100.00%)_Space_29.9_MB_(100.00%)
class Solution {
/**
* @param Integer[] $temperatures
* @return Integer[]
*/
public function dailyTemperatures($temperatures) {
$sol = array_fill(0, count($temperatures), 0);
$sol[count($temperatures) - 1] = 0;
for ($i = count($sol) - 2; $i >= 0; $i--) {
$j = $i + 1;
while ($j <= count($sol)) {
if ($temperatures[$i] < $temperatures[$j]) {
$sol[$i] = $j - $i;
break;
} else {
if ($sol[$j] == 0) {
break;
}
$j = $j + $sol[$j];
}
}
}
return $sol;
}
}