We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 81230ba commit ed8dff9Copy full SHA for ed8dff9
src/Algorithms/0135.candy/candy.php
@@ -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