Skip to content

Commit ed8dff9

Browse files
committed
Add solution 135.
1 parent 81230ba commit ed8dff9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Algorithms/0135.candy/candy.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer[] $ratings
5+
* @return Integer
6+
*/
7+
function candy($ratings) {
8+
$n = count($ratings);
9+
$sum = 0;
10+
$candies = array_fill(0, $n, 1);
11+
12+
for($i = 1; $i < $n; $i++)
13+
{
14+
if($ratings[$i] > $ratings[$i - 1])
15+
{
16+
$candies[$i] = $candies[$i - 1] + 1;
17+
}
18+
}
19+
20+
for($i = $n - 1; $i > 0; $i--)
21+
{
22+
if($ratings[$i - 1] > $ratings[$i] && $candies[$i - 1] <= $candies[$i])
23+
{
24+
$candies[$i - 1] = $candies[$i] + 1;
25+
}
26+
}
27+
28+
foreach($candies as $candy)
29+
$sum += $candy;
30+
31+
return $sum;
32+
}
33+
}

0 commit comments

Comments
 (0)